Browse Source

fixed push pop mechanism

Daniel Zlotin 8 years ago
parent
commit
212c496385

+ 4
- 3
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -13,6 +13,10 @@ public class NavigationCommandsHandler {
13 13
 
14 14
     private static final String ACTIVITY_PARAMS_BUNDLE = "ACTIVITY_PARAMS_BUNDLE";
15 15
 
16
+    static ActivityParams getActivityParams(Intent intent) {
17
+        return ActivityParamsParser.parse(intent.getBundleExtra(NavigationCommandsHandler.ACTIVITY_PARAMS_BUNDLE));
18
+    }
19
+
16 20
     /**
17 21
      * start a new activity with CLEAR_TASK | NEW_TASK
18 22
      *
@@ -40,9 +44,6 @@ public class NavigationCommandsHandler {
40 44
         });
41 45
     }
42 46
 
43
-    static ActivityParams getActivityParams(Intent intent) {
44
-        return ActivityParamsParser.parse(intent.getBundleExtra(NavigationCommandsHandler.ACTIVITY_PARAMS_BUNDLE));
45
-    }
46 47
 
47 48
     public static void pop(Bundle screenParams) {
48 49
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Screen.java View File

@@ -5,5 +5,7 @@ import android.view.View;
5 5
 public interface Screen {
6 6
     View asView();
7 7
 
8
+    void preventUnmountOnDetachedFromWindow();
9
+
8 10
     void ensureUnmountOnDetachedFromWindow();
9 11
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenImpl.java View File

@@ -122,4 +122,9 @@ public class ScreenImpl extends RelativeLayout implements Screen, ScrollDirectio
122 122
     public void ensureUnmountOnDetachedFromWindow() {
123 123
         contentView.ensureUnmountOnDetachedFromWindow();
124 124
     }
125
+
126
+    @Override
127
+    public void preventUnmountOnDetachedFromWindow() {
128
+        contentView.preventUnmountOnDetachedFromWindow();
129
+    }
125 130
 }

+ 31
- 11
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenStack.java View File

@@ -21,16 +21,38 @@ public class ScreenStack extends FrameLayout {
21 21
     }
22 22
 
23 23
     public void push(ScreenParams screenParams) {
24
+        Screen previous = stack.peek();
25
+        addScreen(screenParams);
26
+        removePreviousWithoutUnmount(previous);
27
+    }
28
+
29
+    private void addScreen(ScreenParams screenParams) {
24 30
         Screen screen = new ScreenImpl(getContext(), screenParams);
25 31
         addView(screen.asView(), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
26 32
         stack.push(screen);
27 33
     }
28 34
 
29
-    public Screen pop() {
30
-        Screen popped = stack.pop();
31
-        popped.ensureUnmountOnDetachedFromWindow();
32
-        removeView(popped.asView());
33
-        return popped;
35
+    private void removePreviousWithoutUnmount(Screen previous) {
36
+        previous.preventUnmountOnDetachedFromWindow();
37
+        removeView(previous.asView());
38
+    }
39
+
40
+    public void pop() {
41
+        if (!canPop()) {
42
+            throw new RuntimeException("Can't pop ScreenStack of size " + getStackSize());
43
+        }
44
+
45
+        Screen toRemove = stack.pop();
46
+        Screen previous = stack.peek();
47
+
48
+        readdPrevious(previous);
49
+
50
+        toRemove.ensureUnmountOnDetachedFromWindow();
51
+        removeView(toRemove.asView());
52
+    }
53
+
54
+    private void readdPrevious(Screen previous) {
55
+        addView(previous.asView(), new LayoutParams(MATCH_PARENT, MATCH_PARENT));
34 56
     }
35 57
 
36 58
     public void popToRoot() {
@@ -40,9 +62,11 @@ public class ScreenStack extends FrameLayout {
40 62
     }
41 63
 
42 64
     public void destroy() {
43
-        while (!isEmpty()) {
44
-            pop();
65
+        for (Screen screen : stack) {
66
+            screen.ensureUnmountOnDetachedFromWindow();
67
+            removeView(screen.asView());
45 68
         }
69
+        stack.clear();
46 70
     }
47 71
 
48 72
     public boolean isEmpty() {
@@ -53,10 +77,6 @@ public class ScreenStack extends FrameLayout {
53 77
         return stack.size();
54 78
     }
55 79
 
56
-    public Screen peek() {
57
-        return stack.peek();
58
-    }
59
-
60 80
     public boolean canPop() {
61 81
         return getStackSize() > 1;
62 82
     }