Browse Source

android: resume activity fix

Daniel Zlotin 7 years ago
parent
commit
80bad189fa

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

5
 import android.support.test.uiautomator.BySelector;
5
 import android.support.test.uiautomator.BySelector;
6
 import android.support.test.uiautomator.UiDevice;
6
 import android.support.test.uiautomator.UiDevice;
7
 import android.support.test.uiautomator.UiObject;
7
 import android.support.test.uiautomator.UiObject;
8
+import android.support.test.uiautomator.UiObjectNotFoundException;
8
 import android.support.test.uiautomator.UiScrollable;
9
 import android.support.test.uiautomator.UiScrollable;
9
 import android.support.test.uiautomator.UiSelector;
10
 import android.support.test.uiautomator.UiSelector;
10
 import android.support.test.uiautomator.Until;
11
 import android.support.test.uiautomator.Until;
48
     public void _2_relaunchFromBackground() throws Exception {
49
     public void _2_relaunchFromBackground() throws Exception {
49
         launchTheApp();
50
         launchTheApp();
50
         assertMainShown();
51
         assertMainShown();
52
+        push();
53
+        assertPushedScreenShown();
51
 
54
 
52
         uiDevice().pressHome();
55
         uiDevice().pressHome();
53
         uiDevice().pressRecentApps();
56
         uiDevice().pressRecentApps();
54
         elementByText("Playground").click();
57
         elementByText("Playground").click();
55
 
58
 
56
-        assertMainShown();
59
+        assertPushedScreenShown();
57
     }
60
     }
58
 
61
 
59
     @Test
62
     @Test
60
     public void _3_relaunchAfterClose() throws Exception {
63
     public void _3_relaunchAfterClose() throws Exception {
61
         launchTheApp();
64
         launchTheApp();
62
-        assertMainShown();
65
+        push();
66
+        assertPushedScreenShown();
63
 
67
 
64
         uiDevice().pressBack();
68
         uiDevice().pressBack();
65
 
69
 
70
     @Test
74
     @Test
71
     public void _4_deviceOrientationDoesNotDestroyActivity() throws Exception {
75
     public void _4_deviceOrientationDoesNotDestroyActivity() throws Exception {
72
         launchTheApp();
76
         launchTheApp();
73
-        assertMainShown();
74
-        elementByText("PUSH").click();
75
-        assertExists(By.text("Pushed screen"));
77
+        push();
78
+        assertPushedScreenShown();
76
 
79
 
77
         uiDevice().setOrientationLeft();
80
         uiDevice().setOrientationLeft();
78
         Thread.sleep(100);
81
         Thread.sleep(100);
79
 
82
 
80
-        assertExists(By.text("Pushed screen"));
83
+        assertPushedScreenShown();
81
     }
84
     }
82
 
85
 
83
     @Test
86
     @Test
84
     public void _5_relaunchAfterActivityKilledBySystem() throws Exception {
87
     public void _5_relaunchAfterActivityKilledBySystem() throws Exception {
85
         launchTheApp();
88
         launchTheApp();
86
-        assertMainShown();
89
+        push();
90
+        assertPushedScreenShown();
87
 
91
 
88
         uiDevice().pressHome();
92
         uiDevice().pressHome();
89
         uiDevice().executeShellCommand("am kill " + PACKAGE_NAME);
93
         uiDevice().executeShellCommand("am kill " + PACKAGE_NAME);
93
         assertMainShown();
97
         assertMainShown();
94
     }
98
     }
95
 
99
 
100
+    private void push() throws UiObjectNotFoundException {
101
+        elementByText("PUSH").click();
102
+    }
103
+
96
     private void assertMainShown() {
104
     private void assertMainShown() {
97
         assertExists(By.text("React Native Navigation!"));
105
         assertExists(By.text("React Native Navigation!"));
98
     }
106
     }
99
 
107
 
108
+    private void launchTheApp() throws Exception {
109
+        uiDevice().executeShellCommand("am start -n " + PACKAGE_NAME + "/.MainActivity");
110
+        uiDevice().waitForIdle();
111
+        acceptOverlayPermissionIfNeeded();
112
+    }
113
+
100
     private void acceptOverlayPermissionIfNeeded() throws Exception {
114
     private void acceptOverlayPermissionIfNeeded() throws Exception {
101
         if (elementByText("Draw over other apps").exists()) {
115
         if (elementByText("Draw over other apps").exists()) {
102
             if (!elementByText("Playground").exists()) {
116
             if (!elementByText("Playground").exists()) {
109
         }
123
         }
110
     }
124
     }
111
 
125
 
112
-    private void launchTheApp() throws Exception {
113
-        uiDevice().executeShellCommand("am start -n " + PACKAGE_NAME + "/.MainActivity");
114
-        uiDevice().waitForIdle();
115
-        acceptOverlayPermissionIfNeeded();
116
-    }
117
-
118
     private UiDevice uiDevice() {
126
     private UiDevice uiDevice() {
119
         return UiDevice.getInstance(getInstrumentation());
127
         return UiDevice.getInstance(getInstrumentation());
120
     }
128
     }
130
     private void assertExists(BySelector selector) {
138
     private void assertExists(BySelector selector) {
131
         assertThat(uiDevice().wait(Until.hasObject(selector), TIMEOUT)).isTrue();
139
         assertThat(uiDevice().wait(Until.hasObject(selector), TIMEOUT)).isTrue();
132
     }
140
     }
141
+
142
+    private void assertPushedScreenShown() {
143
+        assertExists(By.text("Pushed screen"));
144
+    }
133
 }
145
 }

+ 5
- 2
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

45
         };
45
         };
46
 
46
 
47
         registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
47
         registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
48
-            private AtomicBoolean creating = new AtomicBoolean(true);
49
-            private AtomicLong startTime = new AtomicLong();
48
+            private AtomicBoolean creating = new AtomicBoolean(false);
49
+            private AtomicLong startTime = new AtomicLong(0);
50
 
50
 
51
             @Override
51
             @Override
52
             public void onActivityCreated(final Activity activity, Bundle bundle) {
52
             public void onActivityCreated(final Activity activity, Bundle bundle) {
68
                 }
68
                 }
69
 
69
 
70
                 if (!host.getReactInstanceManager().hasStartedCreatingInitialContext()) {
70
                 if (!host.getReactInstanceManager().hasStartedCreatingInitialContext()) {
71
+                    creating.set(false);
71
                     startTime.set(System.currentTimeMillis());
72
                     startTime.set(System.currentTimeMillis());
72
                     host.getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
73
                     host.getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
73
                         @Override
74
                         @Override
80
                             UiThread.postDelayed(new Runnable() {
81
                             UiThread.postDelayed(new Runnable() {
81
                                 @Override
82
                                 @Override
82
                                 public void run() {
83
                                 public void run() {
84
+                                    android.util.Log.d("DebuggingIsHell", "NavigationApplication:run() " + 1);
83
                                     new NavigationEventEmitter(context).emitAppLaunched();
85
                                     new NavigationEventEmitter(context).emitAppLaunched();
84
                                 }
86
                                 }
85
                             }, diff);
87
                             }, diff);
96
                     UiThread.postDelayed(new Runnable() {
98
                     UiThread.postDelayed(new Runnable() {
97
                         @Override
99
                         @Override
98
                         public void run() {
100
                         public void run() {
101
+                            android.util.Log.d("DebuggingIsHell", "NavigationApplication:run() " + 2);
99
                             new NavigationEventEmitter(host.getReactInstanceManager().getCurrentReactContext()).emitAppLaunched();
102
                             new NavigationEventEmitter(host.getReactInstanceManager().getCurrentReactContext()).emitAppLaunched();
100
                         }
103
                         }
101
                     }, 1000);
104
                     }, 1000);