Reputation:
I have written a PhoneGap Android Plugin and there I open a second activity:
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Context context = cordova.getActivity().getApplicationContext();
Intent intent = new Intent(context, secondActivity.class);
cordova.getActivity().startActivity(intent);
}
});
Now I'd like to close the activity with a button and send the Plugin Result to JavaScript, but Im not able to close the activity and go back to the PhoneGap Application - how can I do this?
I hope somebody can help me. Thanks for all answers.
Upvotes: 2
Views: 4821
Reputation: 11721
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.
Upvotes: 6