PhoneGap Android Plugin - close the plugin Activity

≯℡__Kan透↙ 提交于 2019-12-02 00:16:51

In your plugin, use startActivityForResult from CordovaInterface class instead of startActivity from Android:

this.cordova.startActivityForResult(this,intent,0);

(0 is a int value used to identify the activity started, use other numbers if you need to start multiple activities)

In your activity you add the following function to return a result to the plugin :

public void returnResult(int code, String result) {
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", result);
    setResult(code, returnIntent);
    finish();
}

So when you want to exit your activity you call this function with RESULT_CANCELED or RESULT_OK and a string representing what you want to return.

And finally in your plugin class, add the following function :

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    switch (requestCode) {
    case 0: //integer matching the integer suplied when starting the activity
         if(resultCode == android.app.Activity.RESULT_OK){
             //in case of success return the string to javascript
             String result=intent.getStringExtra("result"); 
             this.callbackContext.success(result);
         }
         else{
             //code launched in case of error
             String message=intent.getStringExtra("result");
             this.callbackContext.error(message);
         }
         break;
    default:
         break;
    }
}

Hope it's what you were looking for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!