Browse Source

Stop calling host.clear() when activity is destroyed (#1838)

This is a breaking change and has to be opt-in. To prevent host.clear from being called,
Override `clearHostOnActivityDestroy` in MainApplication and return false:

```java
    @Override
    public boolean clearHostOnActivityDestroy() {
        return false;
    }
```

If host isn't cleared, the next time the app is opened react context might still be initialized.
In this case we emit appLaunched event which has to be handled in Js:

```js
  Promise.resolve(Navigation.isAppLaunched())
      .then((appLaunched) => {
        if (appLaunched) {
          startApp();
        }
        new NativeEventsReceiver().appLaunched(() => {
          startApp();
        });
      })
```
Guy Carmeli 7 years ago
parent
commit
7461baedc9

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

118
 
118
 
119
     public abstract boolean isDebug();
119
     public abstract boolean isDebug();
120
 
120
 
121
+    public boolean clearHostOnActivityDestroy() {
122
+        return true;
123
+    }
124
+
121
     @Nullable
125
     @Nullable
122
     public abstract List<ReactPackage> createAdditionalReactPackages();
126
     public abstract List<ReactPackage> createAdditionalReactPackages();
123
 }
127
 }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

135
         super.onPause();
135
         super.onPause();
136
         currentActivity = null;
136
         currentActivity = null;
137
         IntentDataHandler.onPause(getIntent());
137
         IntentDataHandler.onPause(getIntent());
138
-        getReactGateway().onPauseActivity();
138
+        getReactGateway().onPauseActivity(this);
139
         NavigationApplication.instance.getActivityCallbacks().onActivityPaused(this);
139
         NavigationApplication.instance.getActivityCallbacks().onActivityPaused(this);
140
         EventBus.instance.unregister(this);
140
         EventBus.instance.unregister(this);
141
     }
141
     }
166
 
166
 
167
     private void destroyJsIfNeeded() {
167
     private void destroyJsIfNeeded() {
168
         if (currentActivity == null || currentActivity.isFinishing()) {
168
         if (currentActivity == null || currentActivity.isFinishing()) {
169
-            getReactGateway().onDestroyApp();
169
+            getReactGateway().onDestroyApp(this);
170
         }
170
         }
171
     }
171
     }
172
 
172
 

+ 1
- 3
android/app/src/main/java/com/reactnativenavigation/controllers/SplashActivity.java View File

27
 
27
 
28
         if (NavigationApplication.instance.getReactGateway().hasStartedCreatingContext()) {
28
         if (NavigationApplication.instance.getReactGateway().hasStartedCreatingContext()) {
29
             NavigationApplication.instance.getEventEmitter().sendAppLaunchedEvent();
29
             NavigationApplication.instance.getEventEmitter().sendAppLaunchedEvent();
30
-            overridePendingTransition(0, 0);
31
-            finish();
32
             return;
30
             return;
33
         }
31
         }
34
 
32
 
38
         }
36
         }
39
 
37
 
40
         if (NavigationApplication.instance.isReactContextInitialized()) {
38
         if (NavigationApplication.instance.isReactContextInitialized()) {
41
-            finish();
39
+            NavigationApplication.instance.getEventEmitter().sendAppLaunchedEvent();
42
             return;
40
             return;
43
         }
41
         }
44
 
42
 

+ 9
- 7
android/app/src/main/java/com/reactnativenavigation/react/NavigationReactGateway.java View File

63
 		getReactInstanceManager().onBackPressed();
63
 		getReactInstanceManager().onBackPressed();
64
 	}
64
 	}
65
 
65
 
66
-	public void onDestroyApp() {
67
-		getReactInstanceManager().onHostDestroy();
68
-		host.clear();
69
-	}
70
-
71
-	public void onPauseActivity() {
72
-		getReactInstanceManager().onHostPause();
66
+	public void onDestroyApp(Activity activity) {
67
+		getReactInstanceManager().onHostDestroy(activity);
68
+        if (NavigationApplication.instance.clearHostOnActivityDestroy()) {
69
+            host.clear();
70
+        }
71
+    }
72
+
73
+	public void onPauseActivity(Activity activity) {
74
+		getReactInstanceManager().onHostPause(activity);
73
 		jsDevReloadHandler.onPauseActivity();
75
 		jsDevReloadHandler.onPauseActivity();
74
 	}
76
 	}
75
 
77
 

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java View File

23
 
23
 
24
     void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler);
24
     void onResumeActivity(Activity activity, DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler);
25
 
25
 
26
-    void onPauseActivity();
26
+    void onPauseActivity(Activity activity);
27
 
27
 
28
-    void onDestroyApp();
28
+    void onDestroyApp(Activity activity);
29
 
29
 
30
     void onBackPressed();
30
     void onBackPressed();
31
 
31