Browse Source

unmount fix

Daniel Zlotin 8 years ago
parent
commit
65fe5346ce

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

@@ -86,6 +86,7 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
86 86
 
87 87
     @ReactMethod
88 88
     public void newStack(final ReadableMap params) {
89
+        NavigationCommandsHandler.newStack(BundleConverter.toBundle(params));
89 90
     }
90 91
 
91 92
     @ReactMethod

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

@@ -117,4 +117,8 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
117 117
     void popToRoot(ScreenParams params) {
118 118
         layout.popToRoot(params);
119 119
     }
120
+
121
+    void newStack(ScreenParams params) {
122
+        layout.newStack(params);
123
+    }
120 124
 }

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -73,4 +73,19 @@ public class NavigationCommandsHandler {
73 73
             }
74 74
         });
75 75
     }
76
+
77
+    public static void newStack(Bundle screenParams) {
78
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
79
+        if (currentActivity == null) {
80
+            return;
81
+        }
82
+
83
+        final ScreenParams params = ScreenParamsParser.parse(screenParams);
84
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
85
+            @Override
86
+            public void run() {
87
+                currentActivity.newStack(params);
88
+            }
89
+        });
90
+    }
76 91
 }

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java View File

@@ -14,4 +14,6 @@ public interface Layout {
14 14
     void pop(ScreenParams params);
15 15
 
16 16
     void popToRoot(ScreenParams params);
17
+
18
+    void newStack(ScreenParams params);
17 19
 }

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/layouts/Screen.java View File

@@ -1,5 +1,8 @@
1 1
 package com.reactnativenavigation.layouts;
2 2
 
3
+/**
4
+ * Must extend View
5
+ */
3 6
 public interface Screen {
4
-    void removeAllReactViews();
7
+    void ensureUnmountOnDetachedFromWindow();
5 8
 }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenImpl.java View File

@@ -105,7 +105,7 @@ public class ScreenImpl extends LinearLayout implements Screen, ScrollDirectionL
105 105
     }
106 106
 
107 107
     @Override
108
-    public void removeAllReactViews() {
109
-        contentView.removeFromParentAndUnmount();
108
+    public void ensureUnmountOnDetachedFromWindow() {
109
+        contentView.ensureUnmountOnDetachedFromWindow();
110 110
     }
111 111
 }

+ 2
- 3
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenStack.java View File

@@ -29,6 +29,7 @@ public class ScreenStack extends FrameLayout {
29 29
 
30 30
     public Screen pop() {
31 31
         Screen popped = stack.pop();
32
+        popped.ensureUnmountOnDetachedFromWindow();
32 33
         removeView((View) popped);
33 34
         return popped;
34 35
     }
@@ -41,10 +42,8 @@ public class ScreenStack extends FrameLayout {
41 42
 
42 43
     public void destroy() {
43 44
         while (!isEmpty()) {
44
-            Screen screen = pop();
45
-            screen.removeAllReactViews();
45
+            pop();
46 46
         }
47
-        removeAllViews();
48 47
     }
49 48
 
50 49
     public boolean isEmpty() {

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -60,4 +60,9 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
60 60
     public void popToRoot(ScreenParams params) {
61 61
         stack.popToRoot();
62 62
     }
63
+
64
+    @Override
65
+    public void newStack(ScreenParams params) {
66
+        stack.destroy();
67
+    }
63 68
 }

+ 2
- 6
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

@@ -2,7 +2,6 @@ package com.reactnativenavigation.views;
2 2
 
3 3
 import android.content.Context;
4 4
 import android.os.Bundle;
5
-import android.view.ViewGroup;
6 5
 
7 6
 import com.facebook.react.ReactInstanceManager;
8 7
 import com.facebook.react.ReactRootView;
@@ -38,14 +37,11 @@ public class ContentView extends ReactRootView {
38 37
         return navigationParams;
39 38
     }
40 39
 
41
-    public void removeFromParentWithoutUnmount() {
42
-        // Hack in order to prevent the react view from getting unmounted
40
+    public void preventUnmountOnDetachedFromWindow() {
43 41
         ReactViewHacks.preventUnmountOnDetachedFromWindow(this);
44
-        ((ViewGroup) getParent()).removeView(this);
45 42
     }
46 43
 
47
-    public void removeFromParentAndUnmount() {
44
+    public void ensureUnmountOnDetachedFromWindow() {
48 45
         ReactViewHacks.ensureUnmountOnDetachedFromWindow(this);
49
-        ((ViewGroup) getParent()).removeView(this);
50 46
     }
51 47
 }

+ 9
- 4
example-redux/src/screens/PushedScreen.js View File

@@ -7,7 +7,7 @@ import {
7 7
   StyleSheet,
8 8
   TextInput
9 9
 } from 'react-native';
10
-import { connect } from 'react-redux';
10
+import {connect} from 'react-redux';
11 11
 import * as counterActions from '../reducers/counter/actions';
12 12
 
13 13
 // this is a traditional React component connected to the redux store
@@ -30,12 +30,17 @@ class PushedScreen extends Component {
30 30
   constructor(props) {
31 31
     super(props);
32 32
     this.bgColor = this.getRandomColor();
33
+    console.log(`constructor ${this.bgColor}`);
34
+  }
35
+
36
+  componentWillUnmount() {
37
+    console.log(`componentWillUnmount ${this.bgColor}`);
33 38
   }
34 39
 
35 40
   getRandomColor() {
36 41
     var letters = '0123456789ABCDEF'.split('');
37 42
     var color = '#';
38
-    for (var i = 0; i < 6; i++ ) {
43
+    for (var i = 0; i < 6; i++) {
39 44
       color += letters[Math.floor(Math.random() * 16)];
40 45
     }
41 46
     return color;
@@ -153,13 +158,13 @@ const styles = StyleSheet.create({
153 158
     textAlign: 'center',
154 159
     fontSize: 18,
155 160
     marginBottom: 10,
156
-    marginTop:10
161
+    marginTop: 10
157 162
   },
158 163
   button: {
159 164
     textAlign: 'center',
160 165
     fontSize: 18,
161 166
     marginBottom: 10,
162
-    marginTop:10,
167
+    marginTop: 10,
163 168
     color: 'blue'
164 169
   }
165 170
 });