如何解决Cordova 应用程序在从另一个活动返回时使用回调时崩溃
在我们的cordova 应用程序中,我们使用的是打开另一个活动的SDK。当活动返回时,我们尝试使用 CallbackContext 将数据发回。我们看到很多时候 CallbackContext currentCallbackContext 为 null 并且应用程序崩溃。我们如何确保 currentCallbackContext 不会丢失?
public class PluginBridge extends CordovaPlugin {
private CallbackContext currentCallbackContext = null;
public void initialize(CordovaInterface cordova,CordovaWebView webView) {
super.initialize(cordova,webView);
Log.d(TAG,"Initializing SDK");
}
public boolean execute(String action,JSONArray args,final CallbackContext callbackContext) throws JSONException {
if(action.equals("init")) {
this.currentCallbackContext = callbackContext;
Intent intent = new Intent("com.plugin.sdk.someActivity");
intent.putExtra("token",token);
cordova.startActivityForResult(this,intent,1);
}
}
@Override
public void onActivityResult(final int requestCode,final int resultCode,final Intent data) {
if(resultCode == cordova.getActivity().RESULT_OK){
PluginResult result = new PluginResult(PluginResult.Status.OK,data.getExtras().getString("data")); // data parameter will be send from the other activity.
result.setKeepCallback(true);
currentCallbackContext.sendpluginResult(result);
return;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。