Browse Source

react lifecycle events fixed on android

Daniel Zlotin 7 years ago
parent
commit
2c4c5c7446

+ 36
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ApplicationLifecycleTest.java View File

@@ -4,7 +4,9 @@ import android.content.Intent;
4 4
 import android.net.Uri;
5 5
 import android.provider.Settings;
6 6
 import android.support.test.uiautomator.By;
7
+import android.view.KeyEvent;
7 8
 
9
+import org.junit.Ignore;
8 10
 import org.junit.Test;
9 11
 
10 12
 import static android.support.test.InstrumentationRegistry.getInstrumentation;
@@ -57,6 +59,40 @@ public class ApplicationLifecycleTest extends BaseTest {
57 59
 		assertMainShown();
58 60
 	}
59 61
 
62
+	@Test
63
+	public void pressingMenuOpensDevMenu() throws Exception {
64
+		device().pressKeyCode(KeyEvent.KEYCODE_MENU);
65
+		assertExists(By.text("Debug JS Remotely"));
66
+	}
67
+
68
+	@Test
69
+	public void pressingRTwiceInSuccessionReloadsReactNative() throws Exception {
70
+		elementByText("PUSH").click();
71
+		assertExists(By.text("Pushed Screen"));
72
+
73
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
74
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
75
+		device().waitForIdle();
76
+		assertMainShown();
77
+	}
78
+
79
+	@Test
80
+	public void pressingRTwiceWithDelayDoesNothing() throws Exception {
81
+		elementByText("PUSH").click();
82
+		assertExists(By.text("Pushed Screen"));
83
+
84
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
85
+		Thread.sleep(1500);
86
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
87
+		assertExists(By.text("Pushed Screen"));
88
+	}
89
+
90
+	@Test
91
+	@Ignore
92
+	public void reloadReactNativeDoesNotCausesLeaks() throws Exception {
93
+		// TODO
94
+	}
95
+
60 96
 	private void killAppSaveInstanceState_ByTogglingPermissions() throws Exception {
61 97
 		device().pressHome();
62 98
 

+ 0
- 37
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ReactEventsTest.java View File

@@ -1,37 +0,0 @@
1
-package com.reactnativenavigation.e2e.androide2e;
2
-
3
-import android.support.test.uiautomator.By;
4
-import android.view.KeyEvent;
5
-
6
-import org.junit.Test;
7
-
8
-public class ReactEventsTest extends BaseTest {
9
-
10
-	@Test
11
-	public void pressingMenuOpensDevMenu() throws Exception {
12
-		device().pressKeyCode(KeyEvent.KEYCODE_MENU);
13
-		assertExists(By.text("Debug JS Remotely"));
14
-	}
15
-
16
-	@Test
17
-	public void pressingRTwiceInSuccessionReloadsReactNative() throws Exception {
18
-		elementByText("PUSH").click();
19
-		assertExists(By.text("Pushed Screen"));
20
-
21
-		device().pressKeyCode(KeyEvent.KEYCODE_R);
22
-		device().pressKeyCode(KeyEvent.KEYCODE_R);
23
-		device().waitForIdle();
24
-		assertMainShown();
25
-	}
26
-
27
-	@Test
28
-	public void pressingRTwiceWithDelayDoesNothing() throws Exception {
29
-		elementByText("PUSH").click();
30
-		assertExists(By.text("Pushed Screen"));
31
-
32
-		device().pressKeyCode(KeyEvent.KEYCODE_R);
33
-		Thread.sleep(1500);
34
-		device().pressKeyCode(KeyEvent.KEYCODE_R);
35
-		assertExists(By.text("Pushed Screen"));
36
-	}
37
-}

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/NavigationActivity.java View File

@@ -35,6 +35,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
35 35
 	@Override
36 36
 	protected void onDestroy() {
37 37
 		super.onDestroy();
38
+		navigator.destroy();
38 39
 		app().getReactGateway().onActivityDestroyed(this);
39 40
 	}
40 41
 

+ 3
- 3
lib/android/app/src/main/java/com/reactnativenavigation/layout/ReactRootViewController.java View File

@@ -59,9 +59,6 @@ public class ReactRootViewController extends ViewController {
59 59
 	@Override
60 60
 	protected View createView() {
61 61
 		reactRootView = new ReactRootView(getActivity());
62
-		Bundle opts = new Bundle();
63
-		opts.putString("containerId", getId());
64
-		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);
65 62
 		reactRootView.setEventListener(new ReactRootView.ReactRootViewEventListener() {
66 63
 			@Override
67 64
 			public void onAttachedToReactInstance(final ReactRootView reactRootView) {
@@ -69,6 +66,9 @@ public class ReactRootViewController extends ViewController {
69 66
 				attachedToReactInstance = true;
70 67
 			}
71 68
 		});
69
+		final Bundle opts = new Bundle();
70
+		opts.putString("containerId", getId());
71
+		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);
72 72
 		return reactRootView;
73 73
 	}
74 74
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -130,7 +130,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
130 130
 	}
131 131
 
132 132
 	private void handle(Runnable task) {
133
-		if (activity() == null) return;
133
+		if (activity() == null || activity().isFinishing()) return;
134 134
 		UiThread.post(task);
135 135
 	}
136 136
 }

+ 3
- 29
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactInitializer.java View File

@@ -2,24 +2,20 @@ package com.reactnativenavigation.react;
2 2
 
3 3
 import com.facebook.react.ReactInstanceManager;
4 4
 import com.facebook.react.bridge.ReactContext;
5
-import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
6 5
 import com.reactnativenavigation.NavigationActivity;
7
-import com.reactnativenavigation.utils.UiThread;
8 6
 
9 7
 public class NavigationReactInitializer implements ReactInstanceManager.ReactInstanceEventListener {
10 8
 
11 9
 	private final ReactInstanceManager reactInstanceManager;
12 10
 	private final DevPermissionRequest devPermissionRequest;
13
-	private final boolean isDebug;
14 11
 	private boolean waitingForAppLaunchEvent = true;
15 12
 
16 13
 	public NavigationReactInitializer(ReactInstanceManager reactInstanceManager, boolean isDebug) {
17 14
 		this.reactInstanceManager = reactInstanceManager;
18 15
 		this.devPermissionRequest = new DevPermissionRequest(isDebug);
19
-		this.isDebug = isDebug;
20 16
 	}
21 17
 
22
-	public void onActivityCreated(NavigationActivity activity) {
18
+	public void onActivityCreated(final NavigationActivity activity) {
23 19
 		reactInstanceManager.addReactInstanceEventListener(this);
24 20
 		waitingForAppLaunchEvent = true;
25 21
 	}
@@ -29,7 +25,7 @@ public class NavigationReactInitializer implements ReactInstanceManager.ReactIns
29 25
 			devPermissionRequest.askPermission(activity);
30 26
 		} else {
31 27
 			reactInstanceManager.onHostResume(activity, activity);
32
-			checkBundleThenPrepareReact(activity);
28
+			prepareReactApp();
33 29
 		}
34 30
 	}
35 31
 
@@ -46,29 +42,7 @@ public class NavigationReactInitializer implements ReactInstanceManager.ReactIns
46 42
 		}
47 43
 	}
48 44
 
49
-	private void checkBundleThenPrepareReact(final NavigationActivity activity) {
50
-		if (isDebug) {
51
-			reactInstanceManager.getDevSupportManager().isPackagerRunning(new PackagerStatusCallback() {
52
-				@Override
53
-				public void onPackagerStatusFetched(final boolean packagerIsRunning) {
54
-					UiThread.post(new Runnable() {
55
-						@Override
56
-						public void run() {
57
-							if (!packagerIsRunning) {
58
-								activity.toast("Packager is not running!");
59
-							} else {
60
-								prepareReactAppWithWorkingBundle();
61
-							}
62
-						}
63
-					});
64
-				}
65
-			});
66
-		} else {
67
-			prepareReactAppWithWorkingBundle();
68
-		}
69
-	}
70
-
71
-	private void prepareReactAppWithWorkingBundle() {
45
+	private void prepareReactApp() {
72 46
 		if (shouldCreateContext()) {
73 47
 			reactInstanceManager.createReactContextInBackground();
74 48
 		} else if (waitingForAppLaunchEvent) {

+ 0
- 14
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactNativeHost.java View File

@@ -4,14 +4,11 @@ import android.app.Application;
4 4
 
5 5
 import com.facebook.react.ReactNativeHost;
6 6
 import com.facebook.react.ReactPackage;
7
-import com.facebook.react.devsupport.RedBoxHandler;
8 7
 import com.facebook.react.shell.MainReactPackage;
9 8
 
10 9
 import java.util.Arrays;
11 10
 import java.util.List;
12 11
 
13
-import javax.annotation.Nullable;
14
-
15 12
 public class NavigationReactNativeHost extends ReactNativeHost {
16 13
 
17 14
 	private final boolean isDebug;
@@ -21,17 +18,6 @@ public class NavigationReactNativeHost extends ReactNativeHost {
21 18
 		this.isDebug = isDebug;
22 19
 	}
23 20
 
24
-	@Override
25
-	public String getJSMainModuleName() {
26
-		return super.getJSMainModuleName();
27
-	}
28
-
29
-	@Nullable
30
-	@Override
31
-	public RedBoxHandler getRedBoxHandler() {
32
-		return super.getRedBoxHandler();
33
-	}
34
-
35 21
 	@Override
36 22
 	public boolean getUseDeveloperSupport() {
37 23
 		return isDebug;

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

@@ -35,6 +35,12 @@ public class Navigator extends ParentController {
35 35
 		return root != null && root.handleBack();
36 36
 	}
37 37
 
38
+	@Override
39
+	public void destroy() {
40
+		modalStack.dismissAll();
41
+		super.destroy();
42
+	}
43
+
38 44
 	/*
39 45
 	 * Navigation methods
40 46
 	 */