Browse Source

Create TopBar react view only once

fixes #1675
Guy Carmeli 7 years ago
parent
commit
24fb8db816

+ 24
- 5
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

6
 import android.support.annotation.NonNull;
6
 import android.support.annotation.NonNull;
7
 import android.support.annotation.Nullable;
7
 import android.support.annotation.Nullable;
8
 import android.support.design.widget.AppBarLayout;
8
 import android.support.design.widget.AppBarLayout;
9
+import android.support.v4.util.Pair;
9
 import android.support.v7.app.ActionBar;
10
 import android.support.v7.app.ActionBar;
10
 import android.view.Gravity;
11
 import android.view.Gravity;
11
 import android.view.ViewGroup;
12
 import android.view.ViewGroup;
34
     protected TopTabs topTabs;
35
     protected TopTabs topTabs;
35
     private VisibilityAnimator visibilityAnimator;
36
     private VisibilityAnimator visibilityAnimator;
36
     @Nullable
37
     @Nullable
37
-    private ContentView reactView;
38
+    private Pair<String, ContentView> reactView;
38
 
39
 
39
     public TopBar(Context context) {
40
     public TopBar(Context context) {
40
         super(context);
41
         super(context);
91
 
92
 
92
     public void setReactView(@NonNull StyleParams styleParams) {
93
     public void setReactView(@NonNull StyleParams styleParams) {
93
         if (styleParams.hasTopBarCustomComponent()) {
94
         if (styleParams.hasTopBarCustomComponent()) {
94
-            reactView = createReactView(styleParams);
95
+            if (isReactViewAlreadySetAndUnchanged(styleParams)) {
96
+                return;
97
+            }
98
+            unmountReactView();
99
+            reactView = new Pair<>(styleParams.topBarReactView, createReactView(styleParams));
95
             if ("fill".equals(styleParams.topBarReactViewAlignment)) {
100
             if ("fill".equals(styleParams.topBarReactViewAlignment)) {
96
-                addReactViewFill(reactView);
101
+                addReactViewFill(reactView.second);
97
             } else {
102
             } else {
98
-                addCenteredReactView(reactView);
103
+                addCenteredReactView(reactView.second);
99
             }
104
             }
105
+        } else {
106
+            unmountReactView();
100
         }
107
         }
101
     }
108
     }
102
 
109
 
110
+    private void unmountReactView() {
111
+        if (reactView == null) return;
112
+        titleBar.removeView(reactView.second);
113
+        reactView.second.unmountReactView();
114
+        reactView = null;
115
+    }
116
+
117
+    private boolean isReactViewAlreadySetAndUnchanged(@NonNull StyleParams styleParams) {
118
+        return reactView != null && styleParams.topBarReactView.equals(reactView.first);
119
+    }
120
+
103
     private ContentView createReactView(StyleParams styleParams) {
121
     private ContentView createReactView(StyleParams styleParams) {
104
         return new ContentView(getContext(),
122
         return new ContentView(getContext(),
105
                 styleParams.topBarReactView,
123
                 styleParams.topBarReactView,
217
 
235
 
218
     public void destroy() {
236
     public void destroy() {
219
         if (reactView != null) {
237
         if (reactView != null) {
220
-            reactView.unmountReactView();
238
+            reactView.second.unmountReactView();
239
+            reactView = null;
221
         }
240
         }
222
         titleBar.destroy();
241
         titleBar.destroy();
223
     }
242
     }