Browse Source

Implement popToRoot

Jason Skuby 8 years ago
parent
commit
b80b8d29e1

+ 9
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

@@ -233,6 +233,15 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
233 233
         return null;
234 234
     }
235 235
 
236
+    @CallSuper
237
+    public Screen popToRoot(String navigatorId) {
238
+        if (mToolbar != null) {
239
+            mToolbar.hideBackButton();
240
+        }
241
+
242
+        return null;
243
+    }
244
+
236 245
     protected abstract String getCurrentNavigatorId();
237 246
 
238 247
     @CallSuper

+ 13
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

@@ -133,6 +133,19 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
133 133
         return null;
134 134
     }
135 135
 
136
+    @Override
137
+    public Screen popToRoot(String navigatorId) {
138
+        super.popToRoot(navigatorId);
139
+        for (ScreenStack stack: mScreenStacks) {
140
+            if (stack.peek().navigatorId.equals(navigatorId)) {
141
+                Screen popped = stack.popToRoot();
142
+                setNavigationStyle(getCurrentScreen());
143
+                return popped;
144
+            }
145
+        }
146
+        return null;
147
+    }
148
+
136 149
     @Override
137 150
     protected Screen getCurrentScreen() {
138 151
         Screen currentScreen = super.getCurrentScreen();

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java View File

@@ -64,6 +64,14 @@ public class SingleScreenActivity extends BaseReactActivity {
64 64
         return popped;
65 65
     }
66 66
 
67
+    @Override
68
+    public Screen popToRoot(String navigatorId) {
69
+        super.popToRoot(navigatorId);
70
+        Screen screen = mScreenStack.popToRoot();
71
+        setNavigationStyle(getCurrentScreen());
72
+        return screen;
73
+    }
74
+
67 75
     @Override
68 76
     public String getCurrentNavigatorId() {
69 77
         return mNavigatorId;

+ 18
- 1
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java View File

@@ -27,6 +27,7 @@ import java.util.ArrayList;
27 27
  */
28 28
 public class RctActivityModule extends ReactContextBaseJavaModule {
29 29
     public static final String REACT_CLASS = "RctActivity";
30
+    private static final String KEY_NAVIGATOR_ID = "navigatorID";
30 31
 
31 32
     public RctActivityModule(ReactApplicationContext reactContext) {
32 33
         super(reactContext);
@@ -133,7 +134,7 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
133 134
 
134 135
     @ReactMethod
135 136
     public void navigatorPop(final ReadableMap navigator) {
136
-        final String navigatorId = navigator.getString("navigatorID");
137
+        final String navigatorId = navigator.getString(KEY_NAVIGATOR_ID);
137 138
         final BaseReactActivity context = ContextProvider.getActivityContext();
138 139
         if (context == null || context.isFinishing()) {
139 140
             return;
@@ -160,6 +161,22 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
160 161
                 }
161 162
             });
162 163
         }
164
+    }
165
+
166
+    @ReactMethod
167
+    public void navigatorPopToRoot(final ReadableMap params) {
168
+        final BaseReactActivity context = ContextProvider.getActivityContext();
169
+        if (context == null || context.isFinishing()) {
170
+            return;
171
+        }
172
+
173
+        final String navigatorID = params.getString(KEY_NAVIGATOR_ID);
174
+        context.runOnUiThread(new Runnable() {
175
+            @Override
176
+            public void run() {
177
+                context.popToRoot(navigatorID);
178
+            }
179
+        });
163 180
 
164 181
     }
165 182
 

+ 27
- 0
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java View File

@@ -81,6 +81,33 @@ public class ScreenStack extends FrameLayout {
81 81
         return popped.screen;
82 82
     }
83 83
 
84
+    public Screen popToRoot() {
85
+        if (mStack.isEmpty()) {
86
+            return null;
87
+        }
88
+
89
+        int stackSize = getStackSize();
90
+        if (stackSize < 2) {
91
+            return null;
92
+        }
93
+
94
+        ScreenView lastView = null;
95
+        while (getStackSize() >= 2) {
96
+            ScreenView popped = mStack.pop();
97
+            ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
98
+            removeView(popped.view);
99
+            if (lastView == null) {
100
+                lastView = popped;
101
+            }
102
+        }
103
+
104
+        if (!mStack.isEmpty()) {
105
+            addView(mStack.peek().view, 0);
106
+        }
107
+
108
+        return lastView.screen;
109
+    }
110
+
84 111
     public boolean isEmpty() {
85 112
         return mStack.isEmpty();
86 113
     }

+ 8
- 0
src/platformSpecific.android.js View File

@@ -69,6 +69,13 @@ function navigatorPop(navigator, params) {
69 69
   RctActivity.navigatorPop(navigator);
70 70
 }
71 71
 
72
+function navigatorPopToRoot(navigator, params) {
73
+  RctActivity.navigatorPopToRoot({
74
+    navigatorID: navigator.navigatorID,
75
+    animated: !(params.animated !== false)
76
+  });
77
+}
78
+
72 79
 function showModal(params) {
73 80
   addNavigatorParams(params);
74 81
   addNavigatorButtons(params);
@@ -117,6 +124,7 @@ export default {
117 124
   startSingleScreenApp,
118 125
   navigatorPush,
119 126
   navigatorPop,
127
+  navigatorPopToRoot,
120 128
   showModal,
121 129
   dismissModal,
122 130
   dismissAllModals,