Browse Source

Destroy reactView in TopBar

Guy Carmeli 7 years ago
parent
commit
087feba29c
1 changed files with 21 additions and 12 deletions
  1. 21
    12
      android/app/src/main/java/com/reactnativenavigation/views/TopBar.java

+ 21
- 12
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -3,9 +3,10 @@ package com.reactnativenavigation.views;
3 3
 import android.content.Context;
4 4
 import android.graphics.Color;
5 5
 import android.os.Build;
6
+import android.support.annotation.NonNull;
7
+import android.support.annotation.Nullable;
6 8
 import android.support.design.widget.AppBarLayout;
7 9
 import android.support.v7.app.ActionBar;
8
-import android.text.TextUtils;
9 10
 import android.view.Gravity;
10 11
 import android.view.ViewGroup;
11 12
 import android.widget.FrameLayout;
@@ -32,6 +33,8 @@ public class TopBar extends AppBarLayout {
32 33
     protected FrameLayout titleBarAndContextualMenuContainer;
33 34
     protected TopTabs topTabs;
34 35
     private VisibilityAnimator visibilityAnimator;
36
+    @Nullable
37
+    private ContentView reactView;
35 38
 
36 39
     public TopBar(Context context) {
37 40
         super(context);
@@ -86,21 +89,25 @@ public class TopBar extends AppBarLayout {
86 89
         titleBar.setSubtitle(subtitle);
87 90
     }
88 91
 
89
-    public void setReactView(StyleParams styleParams) {
90
-        if (!TextUtils.isEmpty(styleParams.topBarReactView)) {
91
-            final ContentView view = new ContentView(getContext(),
92
-                    styleParams.topBarReactView,
93
-                    NavigationParams.EMPTY,
94
-                    styleParams.topBarReactViewInitialProps
95
-            );
92
+    public void setReactView(@NonNull StyleParams styleParams) {
93
+        if (styleParams.hasTopBarCustomComponent()) {
94
+            reactView = createReactView(styleParams);
96 95
             if ("fill".equals(styleParams.topBarReactViewAlignment)) {
97
-                addReactViewFill(view);
96
+                addReactViewFill(reactView);
98 97
             } else {
99
-                addCenteredReactView(view);
98
+                addCenteredReactView(reactView);
100 99
             }
101 100
         }
102 101
     }
103 102
 
103
+    private ContentView createReactView(StyleParams styleParams) {
104
+        return new ContentView(getContext(),
105
+                styleParams.topBarReactView,
106
+                NavigationParams.EMPTY,
107
+                styleParams.topBarReactViewInitialProps
108
+        );
109
+    }
110
+
104 111
     private void addReactViewFill(ContentView view) {
105 112
         view.setLayoutParams(new LayoutParams(MATCH_PARENT, ViewUtils.getToolBarHeight()));
106 113
         titleBar.addView(view);
@@ -187,7 +194,7 @@ public class TopBar extends AppBarLayout {
187 194
                     @Override
188 195
                     public void run() {
189 196
                         if (menuToRemove != null) {
190
-                           titleBarAndContextualMenuContainer.removeView(menuToRemove);
197
+                            titleBarAndContextualMenuContainer.removeView(menuToRemove);
191 198
                         }
192 199
                     }
193 200
                 });
@@ -209,7 +216,9 @@ public class TopBar extends AppBarLayout {
209 216
     }
210 217
 
211 218
     public void destroy() {
212
-
219
+        if (reactView != null) {
220
+            reactView.unmountReactView();
221
+        }
213 222
     }
214 223
 
215 224
     public void onViewPagerScreenChanged(BaseScreenParams screenParams) {