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
 
13
 
14
     private static final String ACTIVITY_PARAMS_BUNDLE = "ACTIVITY_PARAMS_BUNDLE";
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
      * start a new activity with CLEAR_TASK | NEW_TASK
21
      * start a new activity with CLEAR_TASK | NEW_TASK
18
      *
22
      *
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
     public static void pop(Bundle screenParams) {
48
     public static void pop(Bundle screenParams) {
48
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
49
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;

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

5
 public interface Screen {
5
 public interface Screen {
6
     View asView();
6
     View asView();
7
 
7
 
8
+    void preventUnmountOnDetachedFromWindow();
9
+
8
     void ensureUnmountOnDetachedFromWindow();
10
     void ensureUnmountOnDetachedFromWindow();
9
 }
11
 }

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

122
     public void ensureUnmountOnDetachedFromWindow() {
122
     public void ensureUnmountOnDetachedFromWindow() {
123
         contentView.ensureUnmountOnDetachedFromWindow();
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
     }
21
     }
22
 
22
 
23
     public void push(ScreenParams screenParams) {
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
         Screen screen = new ScreenImpl(getContext(), screenParams);
30
         Screen screen = new ScreenImpl(getContext(), screenParams);
25
         addView(screen.asView(), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
31
         addView(screen.asView(), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
26
         stack.push(screen);
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
     public void popToRoot() {
58
     public void popToRoot() {
40
     }
62
     }
41
 
63
 
42
     public void destroy() {
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
     public boolean isEmpty() {
72
     public boolean isEmpty() {
53
         return stack.size();
77
         return stack.size();
54
     }
78
     }
55
 
79
 
56
-    public Screen peek() {
57
-        return stack.peek();
58
-    }
59
-
60
     public boolean canPop() {
80
     public boolean canPop() {
61
         return getStackSize() > 1;
81
         return getStackSize() > 1;
62
     }
82
     }