Browse Source

Merge branch 'KeepScreenStackViews'

Guy Carmeli 8 years ago
parent
commit
3e8dca89f4

+ 10
- 5
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

21
 import java.util.HashMap;
21
 import java.util.HashMap;
22
 import java.util.Map;
22
 import java.util.Map;
23
 
23
 
24
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
25
-
26
 /**
24
 /**
27
  * Created by guyc on 02/04/16.
25
  * Created by guyc on 02/04/16.
28
  */
26
  */
42
     private AHBottomNavigation mBottomNavigation;
40
     private AHBottomNavigation mBottomNavigation;
43
     private FrameLayout mContentFrame;
41
     private FrameLayout mContentFrame;
44
     private ArrayList<ScreenStack> mScreenStacks;
42
     private ArrayList<ScreenStack> mScreenStacks;
45
-    private int mCurrentStackPosition = 0;
43
+    private int mCurrentStackPosition = -1;
46
 
44
 
47
     @Override
45
     @Override
48
     protected void handleOnCreate() {
46
     protected void handleOnCreate() {
177
         if (wasSelected) {
175
         if (wasSelected) {
178
             return;
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
         mCurrentStackPosition = position;
187
         mCurrentStackPosition = position;
183
         StyleHelper.updateStyles(mToolbar, getCurrentScreen());
188
         StyleHelper.updateStyles(mToolbar, getCurrentScreen());
184
 
189
 

+ 23
- 2
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java View File

3
 import android.animation.LayoutTransition;
3
 import android.animation.LayoutTransition;
4
 import android.content.Context;
4
 import android.content.Context;
5
 import android.util.AttributeSet;
5
 import android.util.AttributeSet;
6
+import android.view.ViewGroup;
6
 import android.widget.FrameLayout;
7
 import android.widget.FrameLayout;
7
 
8
 
8
 import com.facebook.react.ReactInstanceManager;
9
 import com.facebook.react.ReactInstanceManager;
10
+import com.facebook.react.ReactRootView;
9
 import com.reactnativenavigation.activities.BaseReactActivity;
11
 import com.reactnativenavigation.activities.BaseReactActivity;
10
 import com.reactnativenavigation.core.RctManager;
12
 import com.reactnativenavigation.core.RctManager;
11
 import com.reactnativenavigation.core.objects.Screen;
13
 import com.reactnativenavigation.core.objects.Screen;
30
     }
32
     }
31
 
33
 
32
     private final Stack<ScreenView> mStack = new Stack<>();
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
     private BaseReactActivity mReactActivity;
36
     private BaseReactActivity mReactActivity;
36
 
37
 
37
     public ScreenStack(BaseReactActivity context) {
38
     public ScreenStack(BaseReactActivity context) {
145
     public Screen peek() {
146
     public Screen peek() {
146
         return mStack.peek().screen;
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
 }