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
 import android.content.Context;
3
 import android.content.Context;
4
 import android.graphics.Color;
4
 import android.graphics.Color;
5
 import android.os.Build;
5
 import android.os.Build;
6
+import android.support.annotation.NonNull;
7
+import android.support.annotation.Nullable;
6
 import android.support.design.widget.AppBarLayout;
8
 import android.support.design.widget.AppBarLayout;
7
 import android.support.v7.app.ActionBar;
9
 import android.support.v7.app.ActionBar;
8
-import android.text.TextUtils;
9
 import android.view.Gravity;
10
 import android.view.Gravity;
10
 import android.view.ViewGroup;
11
 import android.view.ViewGroup;
11
 import android.widget.FrameLayout;
12
 import android.widget.FrameLayout;
32
     protected FrameLayout titleBarAndContextualMenuContainer;
33
     protected FrameLayout titleBarAndContextualMenuContainer;
33
     protected TopTabs topTabs;
34
     protected TopTabs topTabs;
34
     private VisibilityAnimator visibilityAnimator;
35
     private VisibilityAnimator visibilityAnimator;
36
+    @Nullable
37
+    private ContentView reactView;
35
 
38
 
36
     public TopBar(Context context) {
39
     public TopBar(Context context) {
37
         super(context);
40
         super(context);
86
         titleBar.setSubtitle(subtitle);
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
             if ("fill".equals(styleParams.topBarReactViewAlignment)) {
95
             if ("fill".equals(styleParams.topBarReactViewAlignment)) {
97
-                addReactViewFill(view);
96
+                addReactViewFill(reactView);
98
             } else {
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
     private void addReactViewFill(ContentView view) {
111
     private void addReactViewFill(ContentView view) {
105
         view.setLayoutParams(new LayoutParams(MATCH_PARENT, ViewUtils.getToolBarHeight()));
112
         view.setLayoutParams(new LayoutParams(MATCH_PARENT, ViewUtils.getToolBarHeight()));
106
         titleBar.addView(view);
113
         titleBar.addView(view);
187
                     @Override
194
                     @Override
188
                     public void run() {
195
                     public void run() {
189
                         if (menuToRemove != null) {
196
                         if (menuToRemove != null) {
190
-                           titleBarAndContextualMenuContainer.removeView(menuToRemove);
197
+                            titleBarAndContextualMenuContainer.removeView(menuToRemove);
191
                         }
198
                         }
192
                     }
199
                     }
193
                 });
200
                 });
209
     }
216
     }
210
 
217
 
211
     public void destroy() {
218
     public void destroy() {
212
-
219
+        if (reactView != null) {
220
+            reactView.unmountReactView();
221
+        }
213
     }
222
     }
214
 
223
 
215
     public void onViewPagerScreenChanged(BaseScreenParams screenParams) {
224
     public void onViewPagerScreenChanged(BaseScreenParams screenParams) {