Parcourir la source

Fix TitleBar back button in Modal

Guy Carmeli il y a 8 ans
Parent
révision
d6d704450e

+ 6
- 3
android/app/src/main/java/com/reactnativenavigation/controllers/Modal.java Voir le fichier

@@ -39,8 +39,11 @@ public class Modal extends Dialog implements DialogInterface.OnDismissListener,
39 39
     }
40 40
 
41 41
     @Override
42
-    public void onTitleBarBackPress() {
43
-        layout.onTitleBarBackPress();
42
+    public boolean onTitleBarBackPress() {
43
+        if (!layout.onBackPressed()) {
44
+            onBackPressed();
45
+        }
46
+        return true;
44 47
     }
45 48
 
46 49
     public interface OnModalDismissedListener {
@@ -63,7 +66,7 @@ public class Modal extends Dialog implements DialogInterface.OnDismissListener,
63 66
         setCancelable(true);
64 67
         setOnDismissListener(this);
65 68
         requestWindowFeature(Window.FEATURE_NO_TITLE);
66
-        layout = new SingleScreenLayout(getActivity(), screenParams);
69
+        layout = new SingleScreenLayout(getActivity(), screenParams, this);
67 70
         setContentView(layout.asView());
68 71
     }
69 72
 

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/ModalController.java Voir le fichier

@@ -98,7 +98,8 @@ public class ModalController implements ScreenStackContainer, Modal.OnModalDismi
98 98
     }
99 99
 
100 100
     @Override
101
-    public void onTitleBarBackPress() {
101
+    public boolean onTitleBarBackPress() {
102 102
         // Do nothing and let the layout handle the back press
103
+        return false;
103 104
     }
104 105
 }

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java Voir le fichier

@@ -196,10 +196,13 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
196 196
     }
197 197
 
198 198
     @Override
199
-    public void onTitleBarBackPress() {
199
+    public boolean onTitleBarBackPress() {
200 200
         if (getCurrentScreenStack().canPop()) {
201 201
             getCurrentScreenStack().pop();
202 202
             setBottomTabsStyleFromCurrentScreen();
203
+            return true;
203 204
         }
205
+
206
+        return false;
204 207
     }
205 208
 }

+ 13
- 3
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java Voir le fichier

@@ -8,6 +8,7 @@ import com.reactnativenavigation.params.ScreenParams;
8 8
 import com.reactnativenavigation.params.TitleBarButtonParams;
9 9
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
10 10
 import com.reactnativenavigation.screens.ScreenStack;
11
+import com.reactnativenavigation.views.TitleBarBackButtonListener;
11 12
 
12 13
 import java.util.List;
13 14
 
@@ -18,6 +19,12 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
18 19
     private final AppCompatActivity activity;
19 20
     private final ScreenParams screenParams;
20 21
     private ScreenStack stack;
22
+    private TitleBarBackButtonListener titleBarBackButtonListener;
23
+
24
+    public SingleScreenLayout(AppCompatActivity activity, ScreenParams screenParams, TitleBarBackButtonListener titleBarBackButtonListener) {
25
+        this(activity, screenParams);
26
+        this.titleBarBackButtonListener = titleBarBackButtonListener;
27
+    }
21 28
 
22 29
     public SingleScreenLayout(AppCompatActivity activity, ScreenParams screenParams) {
23 30
         super(activity);
@@ -48,6 +55,7 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
48 55
     @Override
49 56
     public void destroy() {
50 57
         stack.destroy();
58
+        removeView(stack);
51 59
     }
52 60
 
53 61
     @Override
@@ -97,9 +105,11 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
97 105
     }
98 106
 
99 107
     @Override
100
-    public void onTitleBarBackPress() {
101
-        if (stack.canPop()) {
102
-            stack.pop();
108
+    public boolean onTitleBarBackPress() {
109
+        if (titleBarBackButtonListener != null) {
110
+            return titleBarBackButtonListener.onTitleBarBackPress();
103 111
         }
112
+
113
+        return onBackPressed();
104 114
     }
105 115
 }

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/views/TitleBarBackButtonListener.java Voir le fichier

@@ -1,5 +1,5 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3 3
 public interface TitleBarBackButtonListener {
4
-    void onTitleBarBackPress();
4
+    boolean onTitleBarBackPress();
5 5
 }