I have an activity Called 'Signature' and i call it from CordovaPlugin;
Plugin.java
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException
{
Intent i = new Intent(context, Signature.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cordova.startActivityForResult(this,i,90);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.d(TAG, "activity result in plugin: requestCode(" + requestCode + "), resultCode(" + resultCode + ")");
if(requestCode == 90) {
if (resultCode == this.cordova.getActivity().RESULT_OK) {
Bundle res = intent.getExtras();
String result = res.getString("results");
Log.d("FIRST", "result:"+result);
this.callbackContext
.success(result.toString());
} else {
this.callbackContext.error("Error");
}
}
Signature.java
private void finishWithResult(String result,int status)
{
Bundle conData = new Bundle();
conData.putString("results", result);
Intent intent = new Intent();
intent.putExtras(conData);
setResult(status, intent);
finish();
}
However when i call "cordova.startActivityForResult" function "onActivityResult" immediately invokes it self. I cannot callback from Activity via finishWithResult. Any advices. Thanks
First of all there was some code missing(return-statement for execute-method) and you have to tell android/cordova-plugin to wait until there is a result sent back to your webview-app by using NO_RESULT and setKeepCallback of PluginResult otherwise cordova/android expects to get an result as soon as execute-method has finished:
Plugin.java:
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException
{
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
callbackContext.sendPluginResult(r);
Intent i = new Intent(context, Signature.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cordova.startActivityForResult(this,i,90);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
// here is your former code
...
...
// at last call sendPluginResult
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result.toString()));
// when there is no direct result form your execute-method use sendPluginResult because most plugins I saw and made recently (Reminder) prefer sendPluginResult to success/error
// this.callbackContext.success(result.toString());
}
Have an example here(for your plugin class) and here(for your signature class).
And one of mine: here and here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With