Browse Source

Experimental change to StackView. Prevent screens from getting unmounted when removing them from screen

Guy Carmeli 8 years ago
parent
commit
882e3f0dbc

+ 24
- 0
android/app/src/main/java/com/reactnativenavigation/utils/ReflectionUtils.java View File

@@ -0,0 +1,24 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import java.lang.reflect.Field;
4
+
5
+/**
6
+ * Created by guyc on 14/04/16.
7
+ */
8
+public class ReflectionUtils {
9
+
10
+    public static boolean setBooleanField(Object obj, String name, Boolean value) {
11
+        Field field;
12
+        try {
13
+            field = obj.getClass().getDeclaredField(name);
14
+            field.setAccessible(true);
15
+            field.set(obj, value);
16
+            return true;
17
+        } catch (NoSuchFieldException e) {
18
+            e.printStackTrace();
19
+        } catch (IllegalAccessException e) {
20
+            e.printStackTrace();
21
+        }
22
+        return false;
23
+    }
24
+}

+ 10
- 4
android/app/src/main/java/com/reactnativenavigation/views/RctView.java View File

@@ -13,12 +13,18 @@ import com.reactnativenavigation.core.objects.Screen;
13 13
  */
14 14
 public class RctView extends FrameLayout {
15 15
 
16
+    private ReactRootView mReactRootView;
17
+
18
+    public ReactRootView getReactRootView() {
19
+        return mReactRootView;
20
+    }
21
+
16 22
     public RctView(BaseReactActivity ctx, ReactInstanceManager rctInstanceManager, Screen screen) {
17 23
         super(ctx);
18 24
         setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
19 25
 
20
-        ReactRootView root = new ReactRootView(ctx);
21
-        root.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
26
+        mReactRootView = new ReactRootView(ctx);
27
+        mReactRootView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
22 28
 
23 29
         String componentName = screen.screenId;
24 30
         Bundle passProps = new Bundle();
@@ -26,9 +32,9 @@ public class RctView extends FrameLayout {
26 32
         passProps.putString(Screen.KEY_NAVIGATOR_ID, screen.navigatorId);
27 33
         passProps.putString(Screen.KEY_NAVIGATOR_EVENT_ID, screen.navigatorEventId);
28 34
 
29
-        root.startReactApplication(rctInstanceManager, componentName, passProps);
35
+        mReactRootView.startReactApplication(rctInstanceManager, componentName, passProps);
30 36
 
31
-        addView(root);
37
+        addView(mReactRootView);
32 38
     }
33 39
 }
34 40
 

+ 8
- 9
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java View File

@@ -5,13 +5,15 @@ import android.view.View;
5 5
 import android.widget.FrameLayout;
6 6
 
7 7
 import com.facebook.react.ReactInstanceManager;
8
+import com.facebook.react.ReactRootView;
8 9
 import com.reactnativenavigation.activities.BaseReactActivity;
9 10
 import com.reactnativenavigation.core.RctManager;
10 11
 import com.reactnativenavigation.core.objects.Screen;
12
+import com.reactnativenavigation.utils.ReflectionUtils;
11 13
 
12 14
 import java.util.Stack;
13 15
 
14
-import static android.view.ViewGroup.LayoutParams.*;
16
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
15 17
 
16 18
 public class ScreenStack extends FrameLayout {
17 19
 
@@ -42,8 +44,10 @@ public class ScreenStack extends FrameLayout {
42 44
         }
43 45
         RctView view = new RctView(reactActivity, mReactInstanceManager, screen);
44 46
         addView(view, MATCH_PARENT, MATCH_PARENT);
45
-        if(oldView!=null) {
46
-            oldView.setVisibility(GONE);
47
+        if(oldView != null) {
48
+            ReactRootView reactRootView = ((RctView) oldView).getReactRootView();
49
+            ReflectionUtils.setBooleanField(reactRootView, "mAttachScheduled", true);
50
+            removeView(oldView);
47 51
         }
48 52
         stack.push(new ScreenView(screen, view));
49 53
     }
@@ -54,12 +58,7 @@ public class ScreenStack extends FrameLayout {
54 58
         }
55 59
         ScreenView popped = stack.pop();
56 60
         if(!stack.isEmpty()) {
57
-            View view = stack.peek().view;
58
-            if(view.getParent() == null)
59
-                addView(stack.peek().view, 0);
60
-            else {
61
-                view.setVisibility(VISIBLE);
62
-            }
61
+            addView(stack.peek().view, 0);
63 62
         }
64 63
         removeView(popped.view);
65 64
         return popped.screen;