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,6 +118,10 @@ public abstract class NavigationApplication extends Application implements React
118 118
 
119 119
     public abstract boolean isDebug();
120 120
 
121
+    public boolean clearHostOnActivityDestroy() {
122
+        return true;
123
+    }
124
+
121 125
     @Nullable
122 126
     public abstract List<ReactPackage> createAdditionalReactPackages();
123 127
 }

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

@@ -135,7 +135,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
135 135
         super.onPause();
136 136
         currentActivity = null;
137 137
         IntentDataHandler.onPause(getIntent());
138
-        getReactGateway().onPauseActivity();
138
+        getReactGateway().onPauseActivity(this);
139 139
         NavigationApplication.instance.getActivityCallbacks().onActivityPaused(this);
140 140
         EventBus.instance.unregister(this);
141 141
     }
@@ -166,7 +166,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
166 166
 
167 167
     private void destroyJsIfNeeded() {
168 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,8 +27,6 @@ public abstract class SplashActivity extends AppCompatActivity {
27 27
 
28 28
         if (NavigationApplication.instance.getReactGateway().hasStartedCreatingContext()) {
29 29
             NavigationApplication.instance.getEventEmitter().sendAppLaunchedEvent();
30
-            overridePendingTransition(0, 0);
31
-            finish();
32 30
             return;
33 31
         }
34 32
 
@@ -38,7 +36,7 @@ public abstract class SplashActivity extends AppCompatActivity {
38 36
         }
39 37
 
40 38
         if (NavigationApplication.instance.isReactContextInitialized()) {
41
-            finish();
39
+            NavigationApplication.instance.getEventEmitter().sendAppLaunchedEvent();
42 40
             return;
43 41
         }
44 42
 

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

@@ -63,13 +63,15 @@ public class NavigationReactGateway implements ReactGateway {
63 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 75
 		jsDevReloadHandler.onPauseActivity();
74 76
 	}
75 77
 

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

@@ -23,9 +23,9 @@ public interface ReactGateway {
23 23
 
24 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 30
     void onBackPressed();
31 31