Pārlūkot izejas kodu

Prevent screens pushed to ScreenStack from getting unmounted

Guy Carmeli 8 gadus atpakaļ
vecāks
revīzija
0975ad4020

+ 10
- 5
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java Parādīt failu

@@ -21,8 +21,6 @@ import java.util.ArrayList;
21 21
 import java.util.HashMap;
22 22
 import java.util.Map;
23 23
 
24
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
25
-
26 24
 /**
27 25
  * Created by guyc on 02/04/16.
28 26
  */
@@ -42,7 +40,7 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
42 40
     private AHBottomNavigation mBottomNavigation;
43 41
     private FrameLayout mContentFrame;
44 42
     private ArrayList<ScreenStack> mScreenStacks;
45
-    private int mCurrentStackPosition = 0;
43
+    private int mCurrentStackPosition = -1;
46 44
 
47 45
     @Override
48 46
     protected void handleOnCreate() {
@@ -177,8 +175,15 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
177 175
         if (wasSelected) {
178 176
             return;
179 177
         }
180
-        mContentFrame.removeAllViews();
181
-        mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
178
+
179
+        // Remove current ScreenStack
180
+        if (mCurrentStackPosition >= 0) {
181
+            mScreenStacks.get(mCurrentStackPosition).removeFromScreen(mContentFrame);
182
+        }
183
+
184
+        // Add new ScreenStack
185
+        mScreenStacks.get(position).addToScreen(mContentFrame);
186
+
182 187
         mCurrentStackPosition = position;
183 188
         StyleHelper.updateStyles(mToolbar, getCurrentScreen());
184 189
 

+ 23
- 2
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java Parādīt failu

@@ -3,9 +3,11 @@ package com.reactnativenavigation.views;
3 3
 import android.animation.LayoutTransition;
4 4
 import android.content.Context;
5 5
 import android.util.AttributeSet;
6
+import android.view.ViewGroup;
6 7
 import android.widget.FrameLayout;
7 8
 
8 9
 import com.facebook.react.ReactInstanceManager;
10
+import com.facebook.react.ReactRootView;
9 11
 import com.reactnativenavigation.activities.BaseReactActivity;
10 12
 import com.reactnativenavigation.core.RctManager;
11 13
 import com.reactnativenavigation.core.objects.Screen;
@@ -30,8 +32,7 @@ public class ScreenStack extends FrameLayout {
30 32
     }
31 33
 
32 34
     private final Stack<ScreenView> mStack = new Stack<>();
33
-    private final ReactInstanceManager mReactInstanceManager =
34
-            RctManager.getInstance().getReactInstanceManager();
35
+    private final ReactInstanceManager mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
35 36
     private BaseReactActivity mReactActivity;
36 37
 
37 38
     public ScreenStack(BaseReactActivity context) {
@@ -145,4 +146,24 @@ public class ScreenStack extends FrameLayout {
145 146
     public Screen peek() {
146 147
         return mStack.peek().screen;
147 148
     }
149
+
150
+    /**
151
+     * Remove the ScreenStack from {@code parent} while preventing all child react views from getting unmounted
152
+     */
153
+    public void removeFromScreen(ViewGroup parent) {
154
+        ReactRootView view = mStack.peek().view.getReactRootView();
155
+        ReflectionUtils.setBooleanField(view, "mAttachScheduled", true);
156
+
157
+        parent.removeView(this);
158
+    }
159
+
160
+    /**
161
+     * Add ScreenStack to {@code parent}
162
+     */
163
+    public void addToScreen(ViewGroup parent) {
164
+        ReactRootView view = mStack.peek().view.getReactRootView();
165
+        ReflectionUtils.setBooleanField(view, "mAttachScheduled", false);
166
+
167
+        parent.addView(this, new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
168
+    }
148 169
 }