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
 import android.net.Uri;
4
 import android.net.Uri;
5
 import android.provider.Settings;
5
 import android.provider.Settings;
6
 import android.support.test.uiautomator.By;
6
 import android.support.test.uiautomator.By;
7
+import android.view.KeyEvent;
7
 
8
 
9
+import org.junit.Ignore;
8
 import org.junit.Test;
10
 import org.junit.Test;
9
 
11
 
10
 import static android.support.test.InstrumentationRegistry.getInstrumentation;
12
 import static android.support.test.InstrumentationRegistry.getInstrumentation;
57
 		assertMainShown();
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
 	private void killAppSaveInstanceState_ByTogglingPermissions() throws Exception {
96
 	private void killAppSaveInstanceState_ByTogglingPermissions() throws Exception {
61
 		device().pressHome();
97
 		device().pressHome();
62
 
98
 

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

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
 	@Override
35
 	@Override
36
 	protected void onDestroy() {
36
 	protected void onDestroy() {
37
 		super.onDestroy();
37
 		super.onDestroy();
38
+		navigator.destroy();
38
 		app().getReactGateway().onActivityDestroyed(this);
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
 	@Override
59
 	@Override
60
 	protected View createView() {
60
 	protected View createView() {
61
 		reactRootView = new ReactRootView(getActivity());
61
 		reactRootView = new ReactRootView(getActivity());
62
-		Bundle opts = new Bundle();
63
-		opts.putString("containerId", getId());
64
-		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);
65
 		reactRootView.setEventListener(new ReactRootView.ReactRootViewEventListener() {
62
 		reactRootView.setEventListener(new ReactRootView.ReactRootViewEventListener() {
66
 			@Override
63
 			@Override
67
 			public void onAttachedToReactInstance(final ReactRootView reactRootView) {
64
 			public void onAttachedToReactInstance(final ReactRootView reactRootView) {
69
 				attachedToReactInstance = true;
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
 		return reactRootView;
72
 		return reactRootView;
73
 	}
73
 	}
74
 }
74
 }

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

130
 	}
130
 	}
131
 
131
 
132
 	private void handle(Runnable task) {
132
 	private void handle(Runnable task) {
133
-		if (activity() == null) return;
133
+		if (activity() == null || activity().isFinishing()) return;
134
 		UiThread.post(task);
134
 		UiThread.post(task);
135
 	}
135
 	}
136
 }
136
 }

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

2
 
2
 
3
 import com.facebook.react.ReactInstanceManager;
3
 import com.facebook.react.ReactInstanceManager;
4
 import com.facebook.react.bridge.ReactContext;
4
 import com.facebook.react.bridge.ReactContext;
5
-import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
6
 import com.reactnativenavigation.NavigationActivity;
5
 import com.reactnativenavigation.NavigationActivity;
7
-import com.reactnativenavigation.utils.UiThread;
8
 
6
 
9
 public class NavigationReactInitializer implements ReactInstanceManager.ReactInstanceEventListener {
7
 public class NavigationReactInitializer implements ReactInstanceManager.ReactInstanceEventListener {
10
 
8
 
11
 	private final ReactInstanceManager reactInstanceManager;
9
 	private final ReactInstanceManager reactInstanceManager;
12
 	private final DevPermissionRequest devPermissionRequest;
10
 	private final DevPermissionRequest devPermissionRequest;
13
-	private final boolean isDebug;
14
 	private boolean waitingForAppLaunchEvent = true;
11
 	private boolean waitingForAppLaunchEvent = true;
15
 
12
 
16
 	public NavigationReactInitializer(ReactInstanceManager reactInstanceManager, boolean isDebug) {
13
 	public NavigationReactInitializer(ReactInstanceManager reactInstanceManager, boolean isDebug) {
17
 		this.reactInstanceManager = reactInstanceManager;
14
 		this.reactInstanceManager = reactInstanceManager;
18
 		this.devPermissionRequest = new DevPermissionRequest(isDebug);
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
 		reactInstanceManager.addReactInstanceEventListener(this);
19
 		reactInstanceManager.addReactInstanceEventListener(this);
24
 		waitingForAppLaunchEvent = true;
20
 		waitingForAppLaunchEvent = true;
25
 	}
21
 	}
29
 			devPermissionRequest.askPermission(activity);
25
 			devPermissionRequest.askPermission(activity);
30
 		} else {
26
 		} else {
31
 			reactInstanceManager.onHostResume(activity, activity);
27
 			reactInstanceManager.onHostResume(activity, activity);
32
-			checkBundleThenPrepareReact(activity);
28
+			prepareReactApp();
33
 		}
29
 		}
34
 	}
30
 	}
35
 
31
 
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
 		if (shouldCreateContext()) {
46
 		if (shouldCreateContext()) {
73
 			reactInstanceManager.createReactContextInBackground();
47
 			reactInstanceManager.createReactContextInBackground();
74
 		} else if (waitingForAppLaunchEvent) {
48
 		} else if (waitingForAppLaunchEvent) {

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

4
 
4
 
5
 import com.facebook.react.ReactNativeHost;
5
 import com.facebook.react.ReactNativeHost;
6
 import com.facebook.react.ReactPackage;
6
 import com.facebook.react.ReactPackage;
7
-import com.facebook.react.devsupport.RedBoxHandler;
8
 import com.facebook.react.shell.MainReactPackage;
7
 import com.facebook.react.shell.MainReactPackage;
9
 
8
 
10
 import java.util.Arrays;
9
 import java.util.Arrays;
11
 import java.util.List;
10
 import java.util.List;
12
 
11
 
13
-import javax.annotation.Nullable;
14
-
15
 public class NavigationReactNativeHost extends ReactNativeHost {
12
 public class NavigationReactNativeHost extends ReactNativeHost {
16
 
13
 
17
 	private final boolean isDebug;
14
 	private final boolean isDebug;
21
 		this.isDebug = isDebug;
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
 	@Override
21
 	@Override
36
 	public boolean getUseDeveloperSupport() {
22
 	public boolean getUseDeveloperSupport() {
37
 		return isDebug;
23
 		return isDebug;

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

35
 		return root != null && root.handleBack();
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
 	 * Navigation methods
45
 	 * Navigation methods
40
 	 */
46
 	 */