Parcourir la source

android: fixed application lifecycle

Daniel Zlotin il y a 7 ans
Parent
révision
6a38813386

+ 14
- 13
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ApplicationLifecycleTest.java Voir le fichier

@@ -39,11 +39,14 @@ public class ApplicationLifecycleTest {
39 39
 
40 40
     @Before
41 41
     public void beforeEach() {
42
+        reactIdlingResource.start();
42 43
         Espresso.registerIdlingResources(reactIdlingResource);
43 44
     }
44 45
 
45 46
     @After
46 47
     public void afterEach() {
48
+        uiDevice().waitForIdle();
49
+        reactIdlingResource.stop();
47 50
         Espresso.unregisterIdlingResources(reactIdlingResource);
48 51
     }
49 52
 
@@ -70,40 +73,38 @@ public class ApplicationLifecycleTest {
70 73
     }
71 74
 
72 75
     @Test
73
-    public void _2_relaunchFromBackground() throws Exception {
76
+    public void _2_RelaunchFromBackground() throws Exception {
74 77
         rule.launchActivity(null);
78
+
75 79
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
80
+        uiDevice().waitForIdle();
76 81
 
77 82
         uiDevice().pressHome();
78 83
         uiDevice().pressRecentApps();
79 84
         uiDevice().findObject(new UiSelector().text("Playground")).click();
85
+        uiDevice().waitForIdle();
80 86
 
81 87
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
82 88
     }
83 89
 
84 90
     @Test
85
-    public void _3_relaunchAfterClose() throws Exception {
91
+    public void _3_RelaunchAfterClose() throws Exception {
86 92
         rule.launchActivity(null);
93
+        uiDevice().waitForIdle();
94
+
87 95
         uiDevice().pressBack();
96
+        uiDevice().waitForIdle();
97
+
88 98
         rule.launchActivity(null);
99
+        uiDevice().waitForIdle();
100
+
89 101
         onView(withText("React Native Navigation!")).check(matches(isDisplayed()));
90 102
     }
91 103
 }
92
-//    xdescribe('android application lifecycle', () => {
93
-////launch, pause, and resume
94
-//
95
-//        it('launch already running in background', () => {
96
-//        //
97
-//        });
98
-//
99 104
 //        it('launch after activity killed by system', () => {
100 105
 //        //
101 106
 //        });
102 107
 //
103
-//        it('launch and ask react overlay permissions', () => {
104
-//        //
105
-//        });
106
-//
107 108
 //        it('launch after reactContext killed by system', () => {
108 109
 //        //
109 110
 //        });

+ 16
- 3
playground/android/app/src/androidTest/java/com/reactnativenavigation/playground/ReactIdlingResource.java Voir le fichier

@@ -13,16 +13,29 @@ class ReactIdlingResource implements IdlingResource, NotThreadSafeBridgeIdleDebu
13 13
     private ResourceCallback callback;
14 14
     private AtomicBoolean registered = new AtomicBoolean(false);
15 15
     private AtomicBoolean bridgeIdle = new AtomicBoolean(false);
16
+    private AtomicBoolean shouldRun = new AtomicBoolean(false);
16 17
 
17 18
     ReactIdlingResource() {
19
+    }
20
+
21
+    public void start() {
22
+        shouldRun.set(true);
18 23
         UiThread.postDelayed(new Runnable() {
19 24
             @Override
20 25
             public void run() {
21
-                if (!isIdleNow()) {
22
-                    UiThread.postDelayed(this, 10);
26
+                if (shouldRun.get()) {
27
+                    isIdleNow();
28
+                    UiThread.postDelayed(this, 100);
23 29
                 }
24 30
             }
25
-        }, 10);
31
+        }, 100);
32
+    }
33
+
34
+    public void stop() {
35
+        shouldRun.set(false);
36
+        ReactNativeHost host = NavigationApplication.instance.getReactNativeHost();
37
+        if (host != null && host.getReactInstanceManager().getCurrentReactContext() != null)
38
+            host.getReactInstanceManager().getCurrentReactContext().getCatalystInstance().removeBridgeIdleDebugListener(this);
26 39
     }
27 40
 
28 41
 

+ 2
- 1
playground/android/app/src/main/AndroidManifest.xml Voir le fichier

@@ -11,7 +11,8 @@
11 11
         android:theme="@style/AppTheme">
12 12
         <activity
13 13
             android:name=".MainActivity"
14
-            android:label="@string/app_name">
14
+            android:label="@string/app_name"
15
+            android:launchMode="singleTask">
15 16
             <intent-filter>
16 17
                 <action android:name="android.intent.action.MAIN" />
17 18
                 <category android:name="android.intent.category.LAUNCHER" />

+ 0
- 1
playground/android/app/src/main/java/com/reactnativenavigation/playground/MainActivity.java Voir le fichier

@@ -3,5 +3,4 @@ package com.reactnativenavigation.playground;
3 3
 import com.reactnativenavigation.NavigationActivity;
4 4
 
5 5
 public class MainActivity extends NavigationActivity {
6
-
7 6
 }