Callback from activities in Cordoba

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 the function "cordova.startActivityForResult", the onActivityResult function immediately calls it. I cannot call back from Activity via finishWithResult. Any advice. Thanks

+5
source share
1 answer

First of all, some kind of code (return-statement for execute-method) was missing, and you should say that Android and the cordon plugin should wait until the result is sent back to your web application using NO_RESULT and setKeepCallback from PluginResult otherwise cordova / android expects to get the result as soon as the method completes:

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()); } 

Give an example here (for your plugin class) and here (for your signature class).

And one of mine: here and here .

+6
source

Source: https://habr.com/ru/post/1215534/


All Articles