You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2015/03/31 19:58:39 UTC

android commit: CB-8768 Fix onActivityResult called before plugins are loaded (after MainActivity gets killed)

Repository: cordova-android
Updated Branches:
  refs/heads/master b8f2b8948 -> 1aaba440b


CB-8768 Fix onActivityResult called before plugins are loaded (after MainActivity gets killed)

situation: one of the plugins launches startActivityForResult and the Android OS decides to kill our MainActivity.
once the launched activity is fulfilled it comes back to our MainActivity, which has to be recreated first.
unfortunately Android calls onActivityResult before our Activity has fully loaded our installed plugins.

close #171


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/1aaba440
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/1aaba440
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/1aaba440

Branch: refs/heads/master
Commit: 1aaba440b53b0622cff0bde3e143ba107c23c30a
Parents: b8f2b89
Author: Serge Huijben <s....@gmail.com>
Authored: Tue Mar 31 08:28:13 2015 +0200
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue Mar 31 13:58:22 2015 -0400

----------------------------------------------------------------------
 .../src/org/apache/cordova/CordovaActivity.java |  2 +-
 .../apache/cordova/CordovaInterfaceImpl.java    | 37 ++++++++++++++++----
 2 files changed, 31 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/1aaba440/framework/src/org/apache/cordova/CordovaActivity.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java
index a40aaa6..8e185dd 100755
--- a/framework/src/org/apache/cordova/CordovaActivity.java
+++ b/framework/src/org/apache/cordova/CordovaActivity.java
@@ -138,7 +138,7 @@ public class CordovaActivity extends Activity {
         if (!appView.isInitialized()) {
             appView.init(cordovaInterface, pluginEntries, preferences);
         }
-        cordovaInterface.setPluginManager(appView.getPluginManager());
+        cordovaInterface.onCordovaInit(appView.getPluginManager());
 
         // Wire the hardware volume controls to control media if desired.
         String volumePref = preferences.getString("DefaultVolumeStream", "");

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/1aaba440/framework/src/org/apache/cordova/CordovaInterfaceImpl.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaInterfaceImpl.java b/framework/src/org/apache/cordova/CordovaInterfaceImpl.java
index 2dc6b3d..96d316a 100644
--- a/framework/src/org/apache/cordova/CordovaInterfaceImpl.java
+++ b/framework/src/org/apache/cordova/CordovaInterfaceImpl.java
@@ -17,6 +17,7 @@ public class CordovaInterfaceImpl implements CordovaInterface {
     protected ExecutorService threadPool;
     protected PluginManager pluginManager;
 
+    protected ActivityResultHolder savedResult;
     protected CordovaPlugin activityResultCallback;
     protected String initCallbackService;
     protected int activityResultRequestCode;
@@ -30,10 +31,6 @@ public class CordovaInterfaceImpl implements CordovaInterface {
         this.threadPool = threadPool;
     }
 
-    public void setPluginManager(PluginManager pluginManager) {
-        this.pluginManager = pluginManager;
-    }
-
     @Override
     public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
         setActivityResultCallback(command);
@@ -73,6 +70,16 @@ public class CordovaInterfaceImpl implements CordovaInterface {
     }
 
     /**
+     * Dispatches any pending onActivityResult callbacks.
+     */
+    public void onCordovaInit(PluginManager pluginManager) {
+        this.pluginManager = pluginManager;
+        if (savedResult != null) {
+            onActivityResult(savedResult.requestCode, savedResult.resultCode, savedResult.intent);
+        }
+    }
+
+    /**
      * Routes the result to the awaiting plugin. Returns false if no plugin was waiting.
      */
     public boolean onActivityResult(int requestCode, int resultCode, Intent intent) {
@@ -80,17 +87,21 @@ public class CordovaInterfaceImpl implements CordovaInterface {
         if(callback == null && initCallbackService != null) {
             // The application was restarted, but had defined an initial callback
             // before being shut down.
-            callback = pluginManager.getPlugin(initCallbackService);
+            savedResult = new ActivityResultHolder(requestCode, resultCode, intent);
+            if (pluginManager != null) {
+                callback = pluginManager.getPlugin(initCallbackService);
+            }
         }
-        initCallbackService = null;
         activityResultCallback = null;
 
         if (callback != null) {
             Log.d(TAG, "Sending activity result to plugin");
+            initCallbackService = null;
+            savedResult = null;
             callback.onActivityResult(requestCode, resultCode, intent);
             return true;
         }
-        Log.w(TAG, "Got an activity result, but no plugin was registered to receive it.");
+        Log.w(TAG, "Got an activity result, but no plugin was registered to receive it" + (savedResult != null ? " yet!": "."));
         return false;
     }
 
@@ -119,4 +130,16 @@ public class CordovaInterfaceImpl implements CordovaInterface {
     public void restoreInstanceState(Bundle savedInstanceState) {
         initCallbackService = savedInstanceState.getString("callbackService");
     }
+
+    private static class ActivityResultHolder {
+        private int requestCode;
+        private int resultCode;
+        private Intent intent;
+
+        public ActivityResultHolder(int requestCode, int resultCode, Intent intent) {
+            this.requestCode = requestCode;
+            this.resultCode = resultCode;
+            this.intent = intent;
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org