Browse Source

Pop modal stack on back press

Guy Carmeli 7 years ago
parent
commit
fd8cda2c51

+ 10
- 1
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ModalsTest.java View File

@@ -114,6 +114,15 @@ public class ModalsTest extends BaseTest {
114 114
     public void pushIntoModal() throws Exception {
115 115
         elementByText("SHOW MODAL").click();
116 116
         elementByText("PUSH SCREEN").click();
117
-        assertExists(By.text("Pushed from modal"));
117
+        assertExists(By.text("Pushed Screen"));
118
+    }
119
+
120
+    @Test
121
+    public void backPopsModalStack() throws Exception {
122
+        elementByText("SHOW MODAL").click();
123
+        elementByText("PUSH SCREEN").click();
124
+        elementByText("PUSH").click();
125
+        device().pressBack();
126
+        assertExists(By.text("Pushed Screen"));
118 127
     }
119 128
 }

+ 18
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ModalStack.java View File

@@ -1,7 +1,9 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Dialog;
4
+import android.content.DialogInterface;
4 5
 import android.support.annotation.Nullable;
6
+import android.view.KeyEvent;
5 7
 import android.view.View;
6 8
 
7 9
 import com.facebook.react.bridge.Promise;
@@ -65,13 +67,14 @@ public class ModalStack {
65 67
         return modal != null ? modal.viewController : null;
66 68
     }
67 69
 
68
-    private static class Modal {
70
+    private static class Modal implements DialogInterface.OnKeyListener {
69 71
 		public final ViewController viewController;
70 72
 		private final Dialog dialog;
71 73
 
72 74
 		Modal(final ViewController viewController) {
73 75
 			this.viewController = viewController;
74 76
 			dialog = new Dialog(viewController.getActivity(), R.style.Modal);
77
+			dialog.setOnKeyListener(this);
75 78
 		}
76 79
 
77 80
 		void show() {
@@ -92,5 +95,18 @@ public class ModalStack {
92 95
 			View decorView = viewController.getActivity().getWindow().getDecorView();
93 96
 			viewController.getView().measure(makeMeasureSpec(decorView.getMeasuredWidth(), EXACTLY), makeMeasureSpec(decorView.getMeasuredHeight(), EXACTLY));
94 97
 		}
95
-	}
98
+
99
+        @Override
100
+        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
101
+            if (keyCode == KeyEvent.KEYCODE_BACK) {
102
+                if (event.getAction() == KeyEvent.ACTION_UP) {
103
+                    if (viewController.handleBack()) {
104
+                        return true;
105
+                    }
106
+                    dialog.dismiss();
107
+                }
108
+            }
109
+            return false;
110
+        }
111
+    }
96 112
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

@@ -36,7 +36,7 @@ public class Navigator extends ParentController {
36 36
 	@NonNull
37 37
 	@Override
38 38
 	public Collection<ViewController> getChildControllers() {
39
-		return root == null ? Collections.<ViewController>emptyList() : Collections.singletonList(root);
39
+		return root == null ? Collections.emptyList() : Collections.singletonList(root);
40 40
 	}
41 41
 
42 42
 	@Override

+ 6
- 6
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

@@ -94,7 +94,7 @@ public class NavigatorTest extends BaseTest {
94 94
 		StackController stack2 = newStack();
95 95
 		stack1.push(child1);
96 96
 		stack2.push(child2);
97
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
97
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
98 98
 		uut.setRoot(bottomTabsController);
99 99
 
100 100
 		SimpleViewController newChild = new SimpleViewController(activity, "new child");
@@ -121,7 +121,7 @@ public class NavigatorTest extends BaseTest {
121 121
 		stack2.push(child2);
122 122
 		stack2.push(child3);
123 123
 		stack2.push(child4);
124
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
124
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
125 125
 		uut.setRoot(bottomTabsController);
126 126
 
127 127
 		uut.pop("child4");
@@ -138,7 +138,7 @@ public class NavigatorTest extends BaseTest {
138 138
 		stack2.push(child2);
139 139
 		stack2.push(child3);
140 140
 		stack2.push(child4);
141
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
141
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
142 142
 		uut.setRoot(bottomTabsController);
143 143
 
144 144
 		uut.popSpecific(child2.getId());
@@ -156,7 +156,7 @@ public class NavigatorTest extends BaseTest {
156 156
 		stack2.push(child3);
157 157
 		stack2.push(child4);
158 158
 		stack2.push(child5);
159
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
159
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
160 160
 		uut.setRoot(bottomTabsController);
161 161
 
162 162
 		uut.popTo(child2.getId());
@@ -175,7 +175,7 @@ public class NavigatorTest extends BaseTest {
175 175
 		stack2.push(child4);
176 176
 		stack2.push(child5);
177 177
 
178
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
178
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
179 179
 		uut.setRoot(bottomTabsController);
180 180
 
181 181
 		uut.popToRoot(child3.getId());
@@ -272,7 +272,7 @@ public class NavigatorTest extends BaseTest {
272 272
 		stack2.push(child2);
273 273
 		stack2.push(child3);
274 274
 		stack2.push(child4);
275
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
275
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
276 276
 		uut.setRoot(bottomTabsController);
277 277
 
278 278
 		uut.pop("child4", new MockPromise() {

+ 1
- 1
playground/src/containers/ModalScreen.js View File

@@ -83,7 +83,7 @@ class ModalScreen extends Component {
83 83
 
84 84
   onClickPushScreen() {
85 85
     Navigation.push(this.props.containerId, {
86
-      name: `navigation.playground.TextScreen`,
86
+      name: `navigation.playground.PushedScreen`,
87 87
       passProps: {
88 88
         text: 'Pushed from modal'
89 89
       }