瀏覽代碼

work on bridge and react packages with guyc

Daniel Zlotin 8 年之前
父節點
當前提交
51811361f3

+ 65
- 134
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java 查看文件

21
 import com.facebook.common.logging.FLog;
21
 import com.facebook.common.logging.FLog;
22
 import com.facebook.react.ReactInstanceManager;
22
 import com.facebook.react.ReactInstanceManager;
23
 import com.facebook.react.ReactPackage;
23
 import com.facebook.react.ReactPackage;
24
-import com.facebook.react.ReactRootView;
25
 import com.facebook.react.bridge.Arguments;
24
 import com.facebook.react.bridge.Arguments;
26
 import com.facebook.react.bridge.ReadableMap;
25
 import com.facebook.react.bridge.ReadableMap;
27
 import com.facebook.react.bridge.WritableMap;
26
 import com.facebook.react.bridge.WritableMap;
29
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
28
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
30
 import com.facebook.react.shell.MainReactPackage;
29
 import com.facebook.react.shell.MainReactPackage;
31
 import com.reactnativenavigation.BuildConfig;
30
 import com.reactnativenavigation.BuildConfig;
31
+import com.reactnativenavigation.bridge.NavigationReactPackage;
32
 import com.reactnativenavigation.controllers.ModalController;
32
 import com.reactnativenavigation.controllers.ModalController;
33
-import com.reactnativenavigation.core.RctManager;
34
 import com.reactnativenavigation.core.objects.Button;
33
 import com.reactnativenavigation.core.objects.Button;
35
 import com.reactnativenavigation.core.objects.Drawer;
34
 import com.reactnativenavigation.core.objects.Drawer;
36
 import com.reactnativenavigation.core.objects.Screen;
35
 import com.reactnativenavigation.core.objects.Screen;
37
 import com.reactnativenavigation.modal.RnnModal;
36
 import com.reactnativenavigation.modal.RnnModal;
38
-import com.reactnativenavigation.bridge.NavigationReactPackage;
39
 import com.reactnativenavigation.utils.ContextProvider;
37
 import com.reactnativenavigation.utils.ContextProvider;
40
 import com.reactnativenavigation.utils.StyleHelper;
38
 import com.reactnativenavigation.utils.StyleHelper;
41
 import com.reactnativenavigation.views.RnnToolBar;
39
 import com.reactnativenavigation.views.RnnToolBar;
62
             "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
60
             "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
63
 
61
 
64
     @Nullable
62
     @Nullable
65
-    protected ReactInstanceManager mReactInstanceManager;
66
-    private boolean mDoRefresh = false;
67
-    private Menu mMenu;
68
-    protected RnnToolBar mToolbar;
69
-    protected ActionBarDrawerToggle mDrawerToggle;
70
-    protected DrawerLayout mDrawerLayout;
71
-    protected ScreenStack mDrawerStack;
72
-
73
-    /**
74
-     * Returns the name of the bundle in assets. If this is null, and no file path is specified for
75
-     * the bundle, the app will only work with {@code getUseDeveloperSupport} enabled and will
76
-     * always try to load the JS bundle from the packager server.
77
-     * e.g. "index.android.bundle"
78
-     */
79
-    @Nullable
80
-    public String getBundleAssetName() {
81
-        return "index.android.bundle";
82
-    }
83
-
84
-    /**
85
-     * Returns a custom path of the bundle file. This is used in cases the bundle should be loaded
86
-     * from a custom path. By default it is loaded from Android assets, from a path specified
87
-     * by {getBundleAssetName}.
88
-     * e.g. "file://sdcard/myapp_cache/index.android.bundle"
89
-     */
90
-    @Nullable
91
-    public String getJSBundleFile() {
92
-        return null;
93
-    }
94
-
95
-    /**
96
-     * Returns the name of the main module. Determines the URL used to fetch the JS bundle
97
-     * from the packager server. It is only used when dev support is enabled.
98
-     * This is the first file to be executed once the {@link ReactInstanceManager} is created.
99
-     * e.g. "index.android"
100
-     */
101
-    public String getJSMainModuleName() {
102
-        return "index.android";
103
-    }
104
-
105
-    /**
106
-     * Returns the launchOptions which will be passed to the {@link ReactInstanceManager}
107
-     * when the application is started. By default, this will return null and an empty
108
-     * object will be passed to your top level component as its initial props.
109
-     * If your React Native application requires props set outside of JS, override
110
-     * this method to return the Android.os.Bundle of your desired initial props.
111
-     */
112
-    @Nullable
113
-    protected Bundle getLaunchOptions() {
114
-        return null;
115
-    }
116
-
117
-    /**
118
-     * Returns the name of the main component registered from JavaScript.
119
-     * This is used to schedule rendering of the component.
120
-     * e.g. "MoviesApp"
121
-     */
122
-    protected String getMainComponentName() {
123
-        return "";
124
-    }
125
-
126
-    /**
127
-     * Returns whether dev mode should be enabled. This enables e.g. the dev menu.
128
-     */
129
-    public boolean getUseDeveloperSupport() {
130
-        return BuildConfig.DEBUG;
131
-    }
63
+    protected ReactInstanceManager reactInstanceManager;
64
+    private boolean shouldRefreshOnRR = false;
65
+    private Menu menu;
66
+    protected RnnToolBar toolbar;
67
+    protected ActionBarDrawerToggle drawerToggle;
68
+    protected DrawerLayout drawerLayout;
69
+    protected ScreenStack drawerStack;
132
 
70
 
133
     /**
71
     /**
134
      * Returns a list of {@link ReactPackage} used by the app.
72
      * Returns a list of {@link ReactPackage} used by the app.
143
         );
81
         );
144
     }
82
     }
145
 
83
 
146
-    /**
147
-     * A subclass may override this method if it needs to use a custom {@link ReactRootView}.
148
-     */
149
-    protected ReactRootView createRootView() {
150
-        return new ReactRootView(this);
151
-    }
152
-
153
     @Override
84
     @Override
154
     protected void onCreate(Bundle savedInstanceState) {
85
     protected void onCreate(Bundle savedInstanceState) {
155
         super.onCreate(savedInstanceState);
86
         super.onCreate(savedInstanceState);
156
         ContextProvider.setActivityContext(this);
87
         ContextProvider.setActivityContext(this);
157
-        mReactInstanceManager = createReactInstanceManager();
88
+        reactInstanceManager = createReactInstanceManager();
158
         handleOnCreate();
89
         handleOnCreate();
159
 
90
 
160
     }
91
     }
180
     }
111
     }
181
 
112
 
182
     private void permissionToShowRedboxIfNeeded() {
113
     private void permissionToShowRedboxIfNeeded() {
183
-        if (getUseDeveloperSupport() && Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
114
+        if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
184
             Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
115
             Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
185
             startActivity(serviceIntent);
116
             startActivity(serviceIntent);
186
             FLog.w(ReactConstants.TAG, REDBOX_PERMISSION_MESSAGE);
117
             FLog.w(ReactConstants.TAG, REDBOX_PERMISSION_MESSAGE);
193
         super.onResume();
124
         super.onResume();
194
         ContextProvider.setActivityContext(this);
125
         ContextProvider.setActivityContext(this);
195
 
126
 
196
-        if (mReactInstanceManager != null) {
197
-            mReactInstanceManager.onHostResume(this, this);
127
+        if (reactInstanceManager != null) {
128
+            reactInstanceManager.onHostResume(this, this);
198
         }
129
         }
199
     }
130
     }
200
 
131
 
202
     protected void onPause() {
133
     protected void onPause() {
203
         super.onPause();
134
         super.onPause();
204
 
135
 
205
-        if (mReactInstanceManager != null) {
206
-            mReactInstanceManager.onHostPause();
136
+        if (reactInstanceManager != null) {
137
+            reactInstanceManager.onHostPause();
207
         }
138
         }
208
 
139
 
209
         ContextProvider.clearActivityContext();
140
         ContextProvider.clearActivityContext();
215
 
146
 
216
         // Destroy react instance manager only if there are no resumed react activities
147
         // Destroy react instance manager only if there are no resumed react activities
217
         BaseReactActivity activity = ContextProvider.getActivityContext();
148
         BaseReactActivity activity = ContextProvider.getActivityContext();
218
-        if (mReactInstanceManager != null && (activity == null || activity.isFinishing())) {
149
+        if (reactInstanceManager != null && (activity == null || activity.isFinishing())) {
219
             Log.i(TAG, "Destroying ReactInstanceManager");
150
             Log.i(TAG, "Destroying ReactInstanceManager");
220
-            mReactInstanceManager.onHostDestroy();
151
+            reactInstanceManager.onHostDestroy();
221
             RctManager.getInstance().onDestroy();
152
             RctManager.getInstance().onDestroy();
222
         } else {
153
         } else {
223
             Log.d(TAG, "Not destroying ReactInstanceManager");
154
             Log.d(TAG, "Not destroying ReactInstanceManager");
226
 
157
 
227
     @CallSuper
158
     @CallSuper
228
     public void push(Screen screen) {
159
     public void push(Screen screen) {
229
-        StyleHelper.updateStyles(mToolbar, screen);
230
-        if (mToolbar != null) {
231
-            mToolbar.update(screen);
160
+        StyleHelper.updateStyles(toolbar, screen);
161
+        if (toolbar != null) {
162
+            toolbar.update(screen);
232
 
163
 
233
             if (getCurrentNavigatorId().equals(screen.navigatorId) &&
164
             if (getCurrentNavigatorId().equals(screen.navigatorId) &&
234
                     getScreenStackSize() >= 1) {
165
                     getScreenStackSize() >= 1) {
235
-                mToolbar.setNavUpButton(screen);
166
+                toolbar.setNavUpButton(screen);
236
             }
167
             }
237
         }
168
         }
238
     }
169
     }
239
 
170
 
240
     @CallSuper
171
     @CallSuper
241
     public Screen pop(String navigatorId) {
172
     public Screen pop(String navigatorId) {
242
-        if (mToolbar != null &&
173
+        if (toolbar != null &&
243
                 getCurrentNavigatorId().equals(navigatorId) &&
174
                 getCurrentNavigatorId().equals(navigatorId) &&
244
                 getScreenStackSize() <= 2) {
175
                 getScreenStackSize() <= 2) {
245
-            mToolbar.setNavUpButton();
176
+            toolbar.setNavUpButton();
246
         }
177
         }
247
 
178
 
248
         return null;
179
         return null;
250
 
181
 
251
     @CallSuper
182
     @CallSuper
252
     public Screen popToRoot(String navigatorId) {
183
     public Screen popToRoot(String navigatorId) {
253
-        if (mToolbar != null) {
254
-            mToolbar.setNavUpButton();
184
+        if (toolbar != null) {
185
+            toolbar.setNavUpButton();
255
         }
186
         }
256
 
187
 
257
         return null;
188
         return null;
259
 
190
 
260
     @CallSuper
191
     @CallSuper
261
     public Screen resetTo(Screen screen) {
192
     public Screen resetTo(Screen screen) {
262
-        StyleHelper.updateStyles(mToolbar, screen);
263
-        if (mToolbar != null) {
264
-            mToolbar.setNavUpButton();
193
+        StyleHelper.updateStyles(toolbar, screen);
194
+        if (toolbar != null) {
195
+            toolbar.setNavUpButton();
265
         }
196
         }
266
 
197
 
267
         return null;
198
         return null;
286
     @Override
217
     @Override
287
     public void onConfigurationChanged(Configuration newConfig) {
218
     public void onConfigurationChanged(Configuration newConfig) {
288
         super.onConfigurationChanged(newConfig);
219
         super.onConfigurationChanged(newConfig);
289
-        if (mDrawerToggle != null) {
290
-            mDrawerToggle.onConfigurationChanged(newConfig);
220
+        if (drawerToggle != null) {
221
+            drawerToggle.onConfigurationChanged(newConfig);
291
         }
222
         }
292
     }
223
     }
293
 
224
 
294
     @Override
225
     @Override
295
     public boolean onCreateOptionsMenu(Menu menu) {
226
     public boolean onCreateOptionsMenu(Menu menu) {
296
-        mMenu = menu;
227
+        this.menu = menu;
297
         Screen currentScreen = getCurrentScreen();
228
         Screen currentScreen = getCurrentScreen();
298
-        if (mToolbar != null && currentScreen != null && !isFinishing()) {
299
-            mToolbar.setupToolbarButtonsAsync(currentScreen);
229
+        if (toolbar != null && currentScreen != null && !isFinishing()) {
230
+            toolbar.setupToolbarButtonsAsync(currentScreen);
300
         }
231
         }
301
         return super.onCreateOptionsMenu(menu);
232
         return super.onCreateOptionsMenu(menu);
302
     }
233
     }
303
 
234
 
304
     @Override
235
     @Override
305
     public boolean onOptionsItemSelected(MenuItem item) {
236
     public boolean onOptionsItemSelected(MenuItem item) {
306
-        if (mDrawerToggle != null &&
237
+        if (drawerToggle != null &&
307
                 getScreenStackSize() == 1 &&
238
                 getScreenStackSize() == 1 &&
308
-                mDrawerToggle.onOptionsItemSelected(item)) {
239
+                drawerToggle.onOptionsItemSelected(item)) {
309
             return true;
240
             return true;
310
         }
241
         }
311
 
242
 
324
     @Override
255
     @Override
325
     public void onPostCreate(Bundle savedInstanceState) {
256
     public void onPostCreate(Bundle savedInstanceState) {
326
         super.onPostCreate(savedInstanceState);
257
         super.onPostCreate(savedInstanceState);
327
-        if (mDrawerToggle != null) {
328
-            mDrawerToggle.syncState();
258
+        if (drawerToggle != null) {
259
+            drawerToggle.syncState();
329
         }
260
         }
330
     }
261
     }
331
 
262
 
332
     public Menu getMenu() {
263
     public Menu getMenu() {
333
-        return mMenu;
264
+        return menu;
334
     }
265
     }
335
 
266
 
336
     @Override
267
     @Override
337
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
268
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
338
-        if (mReactInstanceManager != null) {
339
-            mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
269
+        if (reactInstanceManager != null) {
270
+            reactInstanceManager.onActivityResult(requestCode, resultCode, data);
340
         }
271
         }
341
     }
272
     }
342
 
273
 
343
     @Override
274
     @Override
344
     public boolean onKeyUp(int keyCode, KeyEvent event) {
275
     public boolean onKeyUp(int keyCode, KeyEvent event) {
345
-        if (mReactInstanceManager != null &&
346
-                mReactInstanceManager.getDevSupportManager().getDevSupportEnabled()) {
276
+        if (reactInstanceManager != null &&
277
+                reactInstanceManager.getDevSupportManager().getDevSupportEnabled()) {
347
             if (keyCode == KeyEvent.KEYCODE_MENU) {
278
             if (keyCode == KeyEvent.KEYCODE_MENU) {
348
-                mReactInstanceManager.showDevOptionsDialog();
279
+                reactInstanceManager.showDevOptionsDialog();
349
                 return true;
280
                 return true;
350
             }
281
             }
351
             if (keyCode == KeyEvent.KEYCODE_R && !(getCurrentFocus() instanceof EditText)) {
282
             if (keyCode == KeyEvent.KEYCODE_R && !(getCurrentFocus() instanceof EditText)) {
352
                 // Enable double-tap-R-to-reload
283
                 // Enable double-tap-R-to-reload
353
-                if (mDoRefresh) {
354
-                    mReactInstanceManager.getDevSupportManager().handleReloadJS();
355
-                    mDoRefresh = false;
284
+                if (shouldRefreshOnRR) {
285
+                    reactInstanceManager.getDevSupportManager().handleReloadJS();
286
+                    shouldRefreshOnRR = false;
356
                 } else {
287
                 } else {
357
-                    mDoRefresh = true;
288
+                    shouldRefreshOnRR = true;
358
                     new Handler().postDelayed(
289
                     new Handler().postDelayed(
359
                             new Runnable() {
290
                             new Runnable() {
360
                                 @Override
291
                                 @Override
361
                                 public void run() {
292
                                 public void run() {
362
-                                    mDoRefresh = false;
293
+                                    shouldRefreshOnRR = false;
363
                                 }
294
                                 }
364
                             },
295
                             },
365
                             200);
296
                             200);
388
 
319
 
389
         if (getScreenStackSize() > 1) {
320
         if (getScreenStackSize() > 1) {
390
             pop(getCurrentNavigatorId());
321
             pop(getCurrentNavigatorId());
391
-        } else if (mReactInstanceManager != null) {
392
-            mReactInstanceManager.onBackPressed();
322
+        } else if (reactInstanceManager != null) {
323
+            reactInstanceManager.onBackPressed();
393
         } else {
324
         } else {
394
             super.onBackPressed();
325
             super.onBackPressed();
395
         }
326
         }
405
             return;
336
             return;
406
         }
337
         }
407
 
338
 
408
-        mDrawerStack = new ScreenStack(this);
339
+        drawerStack = new ScreenStack(this);
409
         FrameLayout drawerFrame = (FrameLayout) findViewById(drawerFrameId);
340
         FrameLayout drawerFrame = (FrameLayout) findViewById(drawerFrameId);
410
-        drawerFrame.addView(mDrawerStack);
411
-        mDrawerStack.push(drawer.left);
341
+        drawerFrame.addView(drawerStack);
342
+        drawerStack.push(drawer.left);
412
 
343
 
413
-        mDrawerLayout = (DrawerLayout) findViewById(drawerLayoutId);
414
-        mDrawerToggle = mToolbar.setupDrawer(mDrawerLayout, drawer.left, screen);
344
+        drawerLayout = (DrawerLayout) findViewById(drawerLayoutId);
345
+        drawerToggle = toolbar.setupDrawer(drawerLayout, drawer.left, screen);
415
     }
346
     }
416
 
347
 
417
     public void setNavigationButtons(ReadableMap buttons) {
348
     public void setNavigationButtons(ReadableMap buttons) {
418
-        if (mToolbar == null) {
349
+        if (toolbar == null) {
419
             return;
350
             return;
420
         }
351
         }
421
         getCurrentScreen().setButtons(buttons);
352
         getCurrentScreen().setButtons(buttons);
422
-        mToolbar.setupToolbarButtonsAsync(getCurrentScreen());
353
+        toolbar.setupToolbarButtonsAsync(getCurrentScreen());
423
     }
354
     }
424
 
355
 
425
     public void setNavigationTitle(ReadableMap title) {
356
     public void setNavigationTitle(ReadableMap title) {
426
-        if (mToolbar == null) {
357
+        if (toolbar == null) {
427
             return;
358
             return;
428
         }
359
         }
429
 
360
 
430
-        mToolbar.setTitle(title.getString(KEY_TITLE));
361
+        toolbar.setTitle(title.getString(KEY_TITLE));
431
     }
362
     }
432
 
363
 
433
     public void toggleNavigationBar(ReadableMap params) {
364
     public void toggleNavigationBar(ReadableMap params) {
434
-        if (mToolbar == null) {
365
+        if (toolbar == null) {
435
             return;
366
             return;
436
         }
367
         }
437
 
368
 
438
         boolean hide = params.getBoolean(KEY_HIDDEN);
369
         boolean hide = params.getBoolean(KEY_HIDDEN);
439
         boolean animated = params.getBoolean(KEY_ANIMATED);
370
         boolean animated = params.getBoolean(KEY_ANIMATED);
440
         if (hide) {
371
         if (hide) {
441
-            mToolbar.hideToolbar(animated);
372
+            toolbar.hideToolbar(animated);
442
         } else {
373
         } else {
443
-            mToolbar.showToolbar(animated);
374
+            toolbar.showToolbar(animated);
444
         }
375
         }
445
     }
376
     }
446
 
377
 
447
     public void toggleDrawer(ReadableMap params) {
378
     public void toggleDrawer(ReadableMap params) {
448
-        if (mToolbar == null || mDrawerToggle == null) {
379
+        if (toolbar == null || drawerToggle == null) {
449
             return;
380
             return;
450
         }
381
         }
451
 
382
 
454
         String to = params.getString(KEY_TO);
385
         String to = params.getString(KEY_TO);
455
         switch (to) {
386
         switch (to) {
456
             case "open":
387
             case "open":
457
-                mToolbar.showDrawer(animated);
388
+                toolbar.showDrawer(animated);
458
                 break;
389
                 break;
459
             case "closed":
390
             case "closed":
460
-                mToolbar.hideDrawer(animated);
391
+                toolbar.hideDrawer(animated);
461
                 break;
392
                 break;
462
             default:
393
             default:
463
-                mToolbar.toggleDrawer(animated);
394
+                toolbar.toggleDrawer(animated);
464
                 break;
395
                 break;
465
         }
396
         }
466
     }
397
     }

+ 14
- 15
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java 查看文件

11
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
11
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
12
 import com.facebook.react.bridge.ReadableMap;
12
 import com.facebook.react.bridge.ReadableMap;
13
 import com.reactnativenavigation.R;
13
 import com.reactnativenavigation.R;
14
-import com.reactnativenavigation.core.RctManager;
15
 import com.reactnativenavigation.core.objects.Drawer;
14
 import com.reactnativenavigation.core.objects.Drawer;
16
 import com.reactnativenavigation.core.objects.Screen;
15
 import com.reactnativenavigation.core.objects.Screen;
17
 import com.reactnativenavigation.utils.StyleHelper;
16
 import com.reactnativenavigation.utils.StyleHelper;
44
     @Override
43
     @Override
45
     protected void handleOnCreate() {
44
     protected void handleOnCreate() {
46
         super.handleOnCreate();
45
         super.handleOnCreate();
47
-        mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
46
+        reactInstanceManager = RctManager.getInstance().getReactInstanceManager();
48
 
47
 
49
         setContentView(R.layout.bottom_tab_activity);
48
         setContentView(R.layout.bottom_tab_activity);
50
-        mToolbar = (RnnToolBar) findViewById(R.id.toolbar);
49
+        toolbar = (RnnToolBar) findViewById(R.id.toolbar);
51
         mBottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_tab_bar);
50
         mBottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_tab_bar);
52
         mContentFrame = (CoordinatorLayout) findViewById(R.id.contentFrame);
51
         mContentFrame = (CoordinatorLayout) findViewById(R.id.contentFrame);
53
 
52
 
68
     }
67
     }
69
 
68
 
70
     private void setupPages(ArrayList<Screen> screens) {
69
     private void setupPages(ArrayList<Screen> screens) {
71
-        new SetupTabsTask(this, mToolbar, screens).execute();
70
+        new SetupTabsTask(this, toolbar, screens).execute();
72
     }
71
     }
73
 
72
 
74
     private void setupToolbar(ArrayList<Screen> screens) {
73
     private void setupToolbar(ArrayList<Screen> screens) {
75
-        mToolbar.setScreens(screens);
74
+        toolbar.setScreens(screens);
76
         Screen initialScreen = screens.get(0);
75
         Screen initialScreen = screens.get(0);
77
-        mToolbar.update(initialScreen);
78
-        StyleHelper.updateStyles(mToolbar, initialScreen);
76
+        toolbar.update(initialScreen);
77
+        StyleHelper.updateStyles(toolbar, initialScreen);
79
     }
78
     }
80
 
79
 
81
     @Override
80
     @Override
82
     protected void onResume() {
81
     protected void onResume() {
83
         super.onResume();
82
         super.onResume();
84
         if (mScreenStacks != null) {
83
         if (mScreenStacks != null) {
85
-            StyleHelper.updateStyles(mToolbar, getCurrentScreen());
84
+            StyleHelper.updateStyles(toolbar, getCurrentScreen());
86
         }
85
         }
87
     }
86
     }
88
 
87
 
110
                 stack.push(screen);
109
                 stack.push(screen);
111
             }
110
             }
112
         }
111
         }
113
-        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
112
+        StyleHelper.updateStyles(toolbar, getCurrentScreen());
114
 
113
 
115
         if (shouldToggleTabs(screen)) {
114
         if (shouldToggleTabs(screen)) {
116
             toggleTabs(screen.bottomTabsHidden, false);
115
             toggleTabs(screen.bottomTabsHidden, false);
124
             if (stack.peek().navigatorId.equals(navigatorId)) {
123
             if (stack.peek().navigatorId.equals(navigatorId)) {
125
                 Screen popped = stack.pop();
124
                 Screen popped = stack.pop();
126
                 Screen currentScreen = getCurrentScreen();
125
                 Screen currentScreen = getCurrentScreen();
127
-                StyleHelper.updateStyles(mToolbar, currentScreen);
126
+                StyleHelper.updateStyles(toolbar, currentScreen);
128
 
127
 
129
                 if (shouldToggleTabs(currentScreen)) {
128
                 if (shouldToggleTabs(currentScreen)) {
130
                     toggleTabs(currentScreen.bottomTabsHidden, false);
129
                     toggleTabs(currentScreen.bottomTabsHidden, false);
143
             if (stack.peek().navigatorId.equals(navigatorId)) {
142
             if (stack.peek().navigatorId.equals(navigatorId)) {
144
                 Screen popped = stack.popToRoot();
143
                 Screen popped = stack.popToRoot();
145
                 Screen currentScreen = getCurrentScreen();
144
                 Screen currentScreen = getCurrentScreen();
146
-                StyleHelper.updateStyles(mToolbar, currentScreen);
145
+                StyleHelper.updateStyles(toolbar, currentScreen);
147
 
146
 
148
                 if (shouldToggleTabs(currentScreen)) {
147
                 if (shouldToggleTabs(currentScreen)) {
149
                     toggleTabs(currentScreen.bottomTabsHidden, false);
148
                     toggleTabs(currentScreen.bottomTabsHidden, false);
167
 
166
 
168
     public Screen resetTo(Screen screen) {
167
     public Screen resetTo(Screen screen) {
169
         super.resetTo(screen);
168
         super.resetTo(screen);
170
-        StyleHelper.updateStyles(mToolbar, screen);
169
+        StyleHelper.updateStyles(toolbar, screen);
171
         return mScreenStacks.get(mCurrentStackPosition).resetTo(screen);
170
         return mScreenStacks.get(mCurrentStackPosition).resetTo(screen);
172
     }
171
     }
173
 
172
 
196
         mScreenStacks.get(position).addToScreen(mContentFrame);
195
         mScreenStacks.get(position).addToScreen(mContentFrame);
197
 
196
 
198
         mCurrentStackPosition = position;
197
         mCurrentStackPosition = position;
199
-        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
198
+        StyleHelper.updateStyles(toolbar, getCurrentScreen());
200
 
199
 
201
         // Hide or show back button if needed
200
         // Hide or show back button if needed
202
         if (getScreenStackSize() > 1) {
201
         if (getScreenStackSize() > 1) {
203
-            mToolbar.setNavUpButton(getCurrentScreen());
202
+            toolbar.setNavUpButton(getCurrentScreen());
204
         } else {
203
         } else {
205
-            mToolbar.setNavUpButton();
204
+            toolbar.setNavUpButton();
206
         }
205
         }
207
     }
206
     }
208
 
207
 

+ 33
- 0
android/app/src/main/java/com/reactnativenavigation/activities/NavigationActivity.java 查看文件

1
+package com.reactnativenavigation.activities;
2
+
3
+import android.os.Bundle;
4
+
5
+public class NavigationActivity extends BaseReactActivity {
6
+
7
+    @Override
8
+    protected void onCreate(Bundle savedInstanceState) {
9
+        super.onCreate(savedInstanceState);
10
+        startReactContextOnceInBackgroundAndExecuteJS();
11
+    }
12
+
13
+    private void startReactContextOnceInBackgroundAndExecuteJS() {
14
+        if (!getReactInstanceManager().hasStartedCreatingInitialContext()) {
15
+            getReactInstanceManager().createReactContextInBackground();
16
+        }
17
+    }
18
+
19
+    @Override
20
+    protected String getCurrentNavigatorId() {
21
+        return null;
22
+    }
23
+
24
+    @Override
25
+    public int getScreenStackSize() {
26
+        return 0;
27
+    }
28
+
29
+    @Override
30
+    protected void removeAllReactViews() {
31
+
32
+    }
33
+}

+ 0
- 54
android/app/src/main/java/com/reactnativenavigation/activities/RootActivity.java 查看文件

1
-package com.reactnativenavigation.activities;
2
-
3
-import android.view.ViewGroup;
4
-
5
-import com.facebook.react.ReactRootView;
6
-import com.reactnativenavigation.core.objects.Screen;
7
-
8
-/**
9
- * This activity is used to start the JS execution where we load our actual app/screens (index.android.js)
10
- * Triggering react context initialization execute global code before any {@link ReactRootView}
11
- * are displayed.
12
- * <p>Only your MainActivity or activities with {@code action.MAIN} and {@code category.LAUNCHER}
13
- * should extend this activity.
14
- * Created by guyc on 13/04/16.
15
- */
16
-public class RootActivity extends BaseReactActivity {
17
-
18
-    @Override
19
-    protected void handleOnCreate() {
20
-        super.handleOnCreate();
21
-        if (!getReactInstanceManager().hasStartedCreatingInitialContext()) {
22
-            // Trigger react context initialization, global javascript code will now execute
23
-            getReactInstanceManager().createReactContextInBackground();
24
-        }
25
-    }
26
-
27
-    @Override
28
-    protected void onPause() {
29
-        super.onPause();
30
-        finish();
31
-    }
32
-
33
-    // No need to implement stack interface since this activity is only used to start other
34
-    // activities such as TabActivity or SingleScreenActivity.
35
-    @Override
36
-    public Screen getCurrentScreen() {
37
-        return null;
38
-    }
39
-
40
-    @Override
41
-    public String getCurrentNavigatorId() {
42
-        return null;
43
-    }
44
-
45
-    @Override
46
-    public int getScreenStackSize() {
47
-        return 0;
48
-    }
49
-
50
-    @Override
51
-    public void removeAllReactViews() {
52
-
53
-    }
54
-}

+ 7
- 8
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java 查看文件

4
 
4
 
5
 import com.facebook.react.bridge.ReadableMap;
5
 import com.facebook.react.bridge.ReadableMap;
6
 import com.reactnativenavigation.R;
6
 import com.reactnativenavigation.R;
7
-import com.reactnativenavigation.core.RctManager;
8
 import com.reactnativenavigation.core.objects.Drawer;
7
 import com.reactnativenavigation.core.objects.Drawer;
9
 import com.reactnativenavigation.core.objects.Screen;
8
 import com.reactnativenavigation.core.objects.Screen;
10
 import com.reactnativenavigation.utils.StyleHelper;
9
 import com.reactnativenavigation.utils.StyleHelper;
22
     @Override
21
     @Override
23
     protected void handleOnCreate() {
22
     protected void handleOnCreate() {
24
         super.handleOnCreate();
23
         super.handleOnCreate();
25
-        mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
24
+        reactInstanceManager = RctManager.getInstance().getReactInstanceManager();
26
 
25
 
27
         setContentView(R.layout.single_screen_activity);
26
         setContentView(R.layout.single_screen_activity);
28
-        mToolbar = (RnnToolBar) findViewById(R.id.toolbar);
27
+        toolbar = (RnnToolBar) findViewById(R.id.toolbar);
29
 
28
 
30
         final Screen screen = (Screen) getIntent().getSerializableExtra(EXTRA_SCREEN);
29
         final Screen screen = (Screen) getIntent().getSerializableExtra(EXTRA_SCREEN);
31
         final Drawer drawer = (Drawer) getIntent().getSerializableExtra(DRAWER_PARAMS);
30
         final Drawer drawer = (Drawer) getIntent().getSerializableExtra(DRAWER_PARAMS);
50
     }
49
     }
51
 
50
 
52
     protected void setupToolbar(Screen screen) {
51
     protected void setupToolbar(Screen screen) {
53
-        StyleHelper.updateStyles(mToolbar, screen);
52
+        StyleHelper.updateStyles(toolbar, screen);
54
     }
53
     }
55
 
54
 
56
     @Override
55
     @Override
57
     public void push(Screen screen) {
56
     public void push(Screen screen) {
58
         super.push(screen);
57
         super.push(screen);
59
         mScreenStack.push(screen);
58
         mScreenStack.push(screen);
60
-        StyleHelper.updateStyles(mToolbar, screen);
59
+        StyleHelper.updateStyles(toolbar, screen);
61
     }
60
     }
62
 
61
 
63
     @Override
62
     @Override
64
     public Screen pop(String navigatorId) {
63
     public Screen pop(String navigatorId) {
65
         super.pop(navigatorId);
64
         super.pop(navigatorId);
66
         Screen popped = mScreenStack.pop();
65
         Screen popped = mScreenStack.pop();
67
-        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
66
+        StyleHelper.updateStyles(toolbar, getCurrentScreen());
68
         return popped;
67
         return popped;
69
     }
68
     }
70
 
69
 
72
     public Screen popToRoot(String navigatorId) {
71
     public Screen popToRoot(String navigatorId) {
73
         super.popToRoot(navigatorId);
72
         super.popToRoot(navigatorId);
74
         Screen screen = mScreenStack.popToRoot();
73
         Screen screen = mScreenStack.popToRoot();
75
-        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
74
+        StyleHelper.updateStyles(toolbar, getCurrentScreen());
76
         return screen;
75
         return screen;
77
     }
76
     }
78
 
77
 
80
     public Screen resetTo(Screen screen) {
79
     public Screen resetTo(Screen screen) {
81
         super.resetTo(screen);
80
         super.resetTo(screen);
82
         Screen popped = mScreenStack.resetTo(screen);
81
         Screen popped = mScreenStack.resetTo(screen);
83
-        StyleHelper.updateStyles(mToolbar, screen);
82
+        StyleHelper.updateStyles(toolbar, screen);
84
         return popped;
83
         return popped;
85
     }
84
     }
86
 
85
 

+ 7
- 9
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java 查看文件

2
 
2
 
3
 import android.support.v4.view.ViewPager;
3
 import android.support.v4.view.ViewPager;
4
 import android.view.Menu;
4
 import android.view.Menu;
5
-import android.view.ViewGroup;
6
 
5
 
7
 import com.reactnativenavigation.R;
6
 import com.reactnativenavigation.R;
8
 import com.reactnativenavigation.adapters.ViewPagerAdapter;
7
 import com.reactnativenavigation.adapters.ViewPagerAdapter;
9
-import com.reactnativenavigation.core.RctManager;
10
 import com.reactnativenavigation.core.objects.Screen;
8
 import com.reactnativenavigation.core.objects.Screen;
11
 import com.reactnativenavigation.utils.StyleHelper;
9
 import com.reactnativenavigation.utils.StyleHelper;
12
 import com.reactnativenavigation.views.RnnTabLayout;
10
 import com.reactnativenavigation.views.RnnTabLayout;
29
     @Override
27
     @Override
30
     protected void handleOnCreate() {
28
     protected void handleOnCreate() {
31
         super.handleOnCreate();
29
         super.handleOnCreate();
32
-        mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
30
+        reactInstanceManager = RctManager.getInstance().getReactInstanceManager();
33
 
31
 
34
         setContentView(R.layout.tab_activity);
32
         setContentView(R.layout.tab_activity);
35
-        mToolbar = (RnnToolBar) findViewById(R.id.toolbar);
33
+        toolbar = (RnnToolBar) findViewById(R.id.toolbar);
36
         mTabLayout = (RnnTabLayout) findViewById(R.id.tabLayout);
34
         mTabLayout = (RnnTabLayout) findViewById(R.id.tabLayout);
37
         mViewPager = (ViewPager) findViewById(R.id.viewPager);
35
         mViewPager = (ViewPager) findViewById(R.id.viewPager);
38
 
36
 
44
 
42
 
45
     private void setupToolbar(ArrayList<Screen> screens) {
43
     private void setupToolbar(ArrayList<Screen> screens) {
46
         Screen initialScreen = screens.get(0);
44
         Screen initialScreen = screens.get(0);
47
-        mToolbar.setScreens(screens);
48
-        mToolbar.update(initialScreen);
45
+        toolbar.setScreens(screens);
46
+        toolbar.update(initialScreen);
49
         setNavigationStyle(initialScreen);
47
         setNavigationStyle(initialScreen);
50
     }
48
     }
51
 
49
 
54
     }
52
     }
55
 
53
 
56
     private void setupViewPager(ArrayList<Screen> screens) {
54
     private void setupViewPager(ArrayList<Screen> screens) {
57
-        mAdapter = new ViewPagerAdapter(this, mViewPager, mToolbar, screens);
55
+        mAdapter = new ViewPagerAdapter(this, mViewPager, toolbar, screens);
58
         mViewPager.setAdapter(mAdapter);
56
         mViewPager.setAdapter(mAdapter);
59
         mTabLayout.setupWithViewPager(mViewPager);
57
         mTabLayout.setupWithViewPager(mViewPager);
60
         mTabLayout.setOnTabSelectedListener(mAdapter);
58
         mTabLayout.setOnTabSelectedListener(mAdapter);
64
     @Override
62
     @Override
65
     public boolean onCreateOptionsMenu(Menu menu) {
63
     public boolean onCreateOptionsMenu(Menu menu) {
66
         boolean ret = super.onCreateOptionsMenu(menu);
64
         boolean ret = super.onCreateOptionsMenu(menu);
67
-        mToolbar.handleOnCreateOptionsMenuAsync();
65
+        toolbar.handleOnCreateOptionsMenuAsync();
68
         return ret;
66
         return ret;
69
     }
67
     }
70
 
68
 
76
     @Override
74
     @Override
77
     public void push(Screen screen) {
75
     public void push(Screen screen) {
78
         super.push(screen);
76
         super.push(screen);
79
-        StyleHelper.updateStyles(mToolbar, screen);
77
+        StyleHelper.updateStyles(toolbar, screen);
80
         mAdapter.push(screen);
78
         mAdapter.push(screen);
81
     }
79
     }
82
 
80
 

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/adapters/ViewPagerAdapter.java 查看文件

9
 import com.facebook.react.bridge.Arguments;
9
 import com.facebook.react.bridge.Arguments;
10
 import com.facebook.react.bridge.WritableMap;
10
 import com.facebook.react.bridge.WritableMap;
11
 import com.reactnativenavigation.activities.BaseReactActivity;
11
 import com.reactnativenavigation.activities.BaseReactActivity;
12
-import com.reactnativenavigation.core.RctManager;
13
 import com.reactnativenavigation.core.objects.Screen;
12
 import com.reactnativenavigation.core.objects.Screen;
14
 import com.reactnativenavigation.utils.StyleHelper;
13
 import com.reactnativenavigation.utils.StyleHelper;
15
 import com.reactnativenavigation.views.RnnToolBar;
14
 import com.reactnativenavigation.views.RnnToolBar;

+ 24
- 0
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactEventEmitter.java 查看文件

1
+package com.reactnativenavigation.bridge;
2
+
3
+import com.facebook.react.bridge.WritableMap;
4
+import com.facebook.react.modules.core.DeviceEventManagerModule;
5
+import com.reactnativenavigation.core.objects.Screen;
6
+
7
+public class NavigationReactEventEmitter {
8
+
9
+    private static final String KEY_EVENT_ID = "id";
10
+    private static final String KEY_EVENT_TYPE = "type";
11
+    private static final String EVENT_TYPE = "NavBarButtonPress";
12
+
13
+    public void sendEvent(String eventName, String navigatorEventId, WritableMap params) {
14
+        DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter = getEventEmitter();
15
+        if (eventEmitter == null) {
16
+            return;
17
+        }
18
+
19
+        params.putString(KEY_EVENT_TYPE, EVENT_TYPE);
20
+        params.putString(KEY_EVENT_ID, eventName);
21
+        params.putString(Screen.KEY_NAVIGATOR_EVENT_ID, navigatorEventId);
22
+        eventEmitter.emit(navigatorEventId, params);
23
+    }
24
+}

+ 0
- 204
android/app/src/main/java/com/reactnativenavigation/core/RctManager.java 查看文件

1
-package com.reactnativenavigation.core;
2
-
3
-import android.app.Application;
4
-import android.util.Log;
5
-
6
-import com.facebook.react.LifecycleState;
7
-import com.facebook.react.ReactInstanceManager;
8
-import com.facebook.react.ReactPackage;
9
-import com.facebook.react.bridge.JavaJSExecutor;
10
-import com.facebook.react.bridge.ReactContext;
11
-import com.facebook.react.bridge.ReactContextBaseJavaModule;
12
-import com.facebook.react.bridge.WritableMap;
13
-import com.facebook.react.devsupport.DevSupportManager;
14
-import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
15
-import com.reactnativenavigation.activities.BaseReactActivity;
16
-import com.reactnativenavigation.core.objects.Screen;
17
-import com.reactnativenavigation.utils.ContextProvider;
18
-import com.reactnativenavigation.utils.ReflectionUtils;
19
-
20
-import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
21
-
22
-/**
23
- * Created by guyc on 22/02/16.
24
- */
25
-public class RctManager {
26
-    private static final String TAG = "RctManager";
27
-    private static final String KEY_EVENT_ID = "id";
28
-    private static final String KEY_EVENT_TYPE = "type";
29
-    private static final String EVENT_TYPE = "NavBarButtonPress";
30
-    private static RctManager sInstance;
31
-
32
-    private ReactInstanceManager mReactManager;
33
-
34
-    private RctManager() {
35
-        // Singleton
36
-    }
37
-
38
-    public static synchronized RctManager getInstance() {
39
-        if (sInstance == null) {
40
-            sInstance = new RctManager();
41
-        }
42
-        return sInstance;
43
-    }
44
-
45
-    public ReactInstanceManager getReactInstanceManager() {
46
-        return mReactManager;
47
-    }
48
-
49
-    public boolean isInitialized() {
50
-        return mReactManager != null;
51
-    }
52
-
53
-    public void init(BaseReactActivity context) {
54
-        createReactInstanceManager(context);
55
-    }
56
-
57
-    /**
58
-     * Creates a React Instance Manager associated with this component name
59
-     */
60
-    public ReactInstanceManager createReactInstanceManager(BaseReactActivity reactActivity) {
61
-        ReactInstanceManager.Builder builder = ReactInstanceManager.builder()
62
-                .setApplication((Application) reactActivity.getApplicationContext())
63
-                .setJSMainModuleName(reactActivity.getJSMainModuleName())
64
-                .setUseDeveloperSupport(reactActivity.getUseDeveloperSupport())
65
-                .setInitialLifecycleState(LifecycleState.BEFORE_RESUME);
66
-
67
-        for (ReactPackage reactPackage : reactActivity.getPackages()) {
68
-            builder.addPackage(reactPackage);
69
-        }
70
-
71
-        String jsBundleFile = reactActivity.getJSBundleFile();
72
-
73
-        if (jsBundleFile != null) {
74
-            builder.setJSBundleFile(jsBundleFile);
75
-        } else {
76
-            builder.setBundleAssetName(reactActivity.getBundleAssetName());
77
-        }
78
-
79
-        mReactManager = builder.build();
80
-        if (reactActivity.getUseDeveloperSupport()) {
81
-            setupDevSupportHandler(mReactManager);
82
-        }
83
-        return mReactManager;
84
-    }
85
-
86
-    /**
87
-     * Inject our CustomDevCommandsHandler to {@code reactInstanceManager} so we can detect when the bundle was
88
-     * parsed and is about to load.
89
-     * @param reactInstanceManager
90
-     */
91
-    private void setupDevSupportHandler(ReactInstanceManager reactInstanceManager) {
92
-        final ReactInstanceDevCommandsHandler devInterface = (ReactInstanceDevCommandsHandler)
93
-                ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevInterface");
94
-        if (devInterface == null) {
95
-            Log.e(TAG, "Could not get field mDevInterface");
96
-            return;
97
-        }
98
-
99
-        // Create customDevCommandsHandler
100
-        CustomDevCommandsHandler customDevCommandsHandler = new CustomDevCommandsHandler(devInterface);
101
-        boolean success = ReflectionUtils.setField(reactInstanceManager, "mDevInterface", customDevCommandsHandler);
102
-        if (!success) {
103
-            Log.e(TAG, "Could not set field mDevInterface");
104
-            return;
105
-        }
106
-
107
-        // Set customDevCommandsHandler in devSupportManager. Fun =).
108
-        DevSupportManager devSupportManager = (DevSupportManager)
109
-                ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevSupportManager");
110
-        if (devSupportManager == null) {
111
-            Log.e(TAG, "Could not get field mDevSupportManager");
112
-            return;
113
-        }
114
-
115
-        success = ReflectionUtils.setField(devSupportManager, "mReactInstanceCommandsHandler", customDevCommandsHandler);
116
-        if (!success) {
117
-            Log.e(TAG, "Could not set field mReactInstanceCommandsHandler");
118
-        }
119
-    }
120
-
121
-    public <T extends ReactContextBaseJavaModule> T getNativeModule(Class<T> nativeModuleClass) {
122
-        if (mReactManager == null || mReactManager.getCurrentReactContext() == null) {
123
-            return null;
124
-        }
125
-
126
-        return mReactManager.getCurrentReactContext().getNativeModule(nativeModuleClass);
127
-    }
128
-
129
-    /**
130
-     * Sends an event to JavaScript using <a href="https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript">RCTDeviceEventEmitter</a>
131
-     * @param eventName Name of the event
132
-     * @param params Event params
133
-     * @param screen screen which should receive the event
134
-     */
135
-    public void sendEvent(String eventName, Screen screen, WritableMap params) {
136
-        RCTDeviceEventEmitter eventEmitter = getEventEmitter();
137
-        if (eventEmitter == null) {
138
-            return;
139
-        }
140
-
141
-        params.putString(KEY_EVENT_TYPE, EVENT_TYPE);
142
-        params.putString(KEY_EVENT_ID, eventName);
143
-        params.putString(Screen.KEY_NAVIGATOR_EVENT_ID, screen.navigatorEventId);
144
-        eventEmitter.emit(screen.navigatorEventId, params);
145
-    }
146
-
147
-    private RCTDeviceEventEmitter getEventEmitter() {
148
-        if (mReactManager == null) {
149
-            return null;
150
-        }
151
-
152
-        ReactContext currentReactContext = mReactManager.getCurrentReactContext();
153
-        if (currentReactContext == null) {
154
-            return null;
155
-        }
156
-
157
-        return currentReactContext.getJSModule(RCTDeviceEventEmitter.class);
158
-    }
159
-
160
-    public void onDestroy() {
161
-        mReactManager = null;
162
-        sInstance = null;
163
-    }
164
-
165
-    /**
166
-     * Proxy for {@link ReactInstanceDevCommandsHandler} used by {@link DevSupportManager} for requesting React
167
-     * instance recreation. Used to notify {@link BaseReactActivity} that the bundle has been reloaded.
168
-     */
169
-    private static class CustomDevCommandsHandler implements ReactInstanceDevCommandsHandler {
170
-        private ReactInstanceDevCommandsHandler mCommandsHandler;
171
-
172
-        public CustomDevCommandsHandler(ReactInstanceDevCommandsHandler commandsHandler) {
173
-            mCommandsHandler = commandsHandler;
174
-        }
175
-
176
-        @Override
177
-        public void onReloadWithJSDebugger(JavaJSExecutor.Factory proxyExecutorFactory) {
178
-            onJSBundleReloaded();
179
-            mCommandsHandler.onReloadWithJSDebugger(proxyExecutorFactory);
180
-        }
181
-
182
-        @Override
183
-        public void onJSBundleLoadedFromServer() {
184
-            onJSBundleReloaded();
185
-            mCommandsHandler.onJSBundleLoadedFromServer();
186
-        }
187
-
188
-        /**
189
-         * Detach previously added ReactRootViews before handling bundle.
190
-         */
191
-        private void onJSBundleReloaded() {
192
-            BaseReactActivity context = ContextProvider.getActivityContext();
193
-            if (context != null) {
194
-                context.onJSBundleReloaded();
195
-            }
196
-        }
197
-
198
-        @Override
199
-        public void toggleElementInspector() {
200
-            mCommandsHandler.toggleElementInspector();
201
-        }
202
-    }
203
-}
204
-

+ 69
- 0
android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadListenerReplacer.java 查看文件

1
+package com.reactnativenavigation.react;
2
+
3
+import com.facebook.react.ReactInstanceManager;
4
+import com.facebook.react.bridge.JavaJSExecutor;
5
+import com.facebook.react.devsupport.DevSupportManager;
6
+import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
7
+import com.reactnativenavigation.utils.ReflectionUtils;
8
+
9
+public class JsDevReloadListenerReplacer {
10
+    private final ReactInstanceManager reactInstanceManager;
11
+    private final Listener listener;
12
+
13
+    public interface Listener {
14
+        void onJsDevReload();
15
+    }
16
+
17
+    public JsDevReloadListenerReplacer(ReactInstanceManager reactInstanceManager, Listener listener) {
18
+        this.reactInstanceManager = reactInstanceManager;
19
+        this.listener = listener;
20
+    }
21
+
22
+    public void replace() {
23
+        ReactInstanceDevCommandsHandler originalHandler = getOriginalHandler();
24
+        DevCommandsHandlerProxy proxy = new DevCommandsHandlerProxy(originalHandler, listener);
25
+        replaceInReactInstanceManager(proxy);
26
+        replaceInDevSupportManager(proxy);
27
+    }
28
+
29
+    private void replaceInDevSupportManager(DevCommandsHandlerProxy proxy) {
30
+        DevSupportManager devSupportManager = (DevSupportManager)
31
+                ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevSupportManager");
32
+        ReflectionUtils.setField(devSupportManager, "mReactInstanceCommandsHandler", proxy);
33
+    }
34
+
35
+    private ReactInstanceDevCommandsHandler getOriginalHandler() {
36
+        return (ReactInstanceDevCommandsHandler) ReflectionUtils.getDeclaredField(reactInstanceManager, "mDevInterface");
37
+    }
38
+
39
+    private void replaceInReactInstanceManager(DevCommandsHandlerProxy proxy) {
40
+        ReflectionUtils.setField(reactInstanceManager, "mDevInterface", proxy);
41
+    }
42
+
43
+    private static class DevCommandsHandlerProxy implements ReactInstanceDevCommandsHandler {
44
+        private ReactInstanceDevCommandsHandler originalReactHandler;
45
+        private final Listener listener;
46
+
47
+        public DevCommandsHandlerProxy(ReactInstanceDevCommandsHandler originalReactHandler, Listener listener) {
48
+            this.originalReactHandler = originalReactHandler;
49
+            this.listener = listener;
50
+        }
51
+
52
+        @Override
53
+        public void onReloadWithJSDebugger(JavaJSExecutor.Factory proxyExecutorFactory) {
54
+            listener.onJsDevReload();
55
+            originalReactHandler.onReloadWithJSDebugger(proxyExecutorFactory);
56
+        }
57
+
58
+        @Override
59
+        public void onJSBundleLoadedFromServer() {
60
+            listener.onJsDevReload();
61
+            originalReactHandler.onJSBundleLoadedFromServer();
62
+        }
63
+
64
+        @Override
65
+        public void toggleElementInspector() {
66
+            originalReactHandler.toggleElementInspector();
67
+        }
68
+    }
69
+}

+ 66
- 0
android/app/src/main/java/com/reactnativenavigation/react/NavigationReactInstance.java 查看文件

1
+package com.reactnativenavigation.react;
2
+
3
+import android.app.Application;
4
+
5
+import com.facebook.react.LifecycleState;
6
+import com.facebook.react.ReactInstanceManager;
7
+import com.facebook.react.ReactPackage;
8
+import com.facebook.react.bridge.ReactContext;
9
+import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
10
+import com.reactnativenavigation.BuildConfig;
11
+
12
+import java.util.List;
13
+
14
+public class NavigationReactInstance {
15
+
16
+    private final ReactInstanceManager reactInstanceManager;
17
+
18
+    public interface ReactContextCreator {
19
+        Application getApplication();
20
+
21
+        List<ReactPackage> createReactPackages();
22
+
23
+        void onJsDevReload();
24
+    }
25
+
26
+    public NavigationReactInstance(final ReactContextCreator reactContextCreator) {
27
+        reactInstanceManager = createReactInstanceManager(reactContextCreator);
28
+
29
+        if (BuildConfig.DEBUG) {
30
+            replaceJsDevReloadListener(reactContextCreator);
31
+        }
32
+    }
33
+
34
+    private RCTDeviceEventEmitter getEventEmitter() {
35
+        ReactContext currentReactContext = reactInstanceManager.getCurrentReactContext();
36
+        if (currentReactContext == null) {
37
+            return null;
38
+        }
39
+
40
+        return currentReactContext.getJSModule(RCTDeviceEventEmitter.class);
41
+    }
42
+
43
+    private void replaceJsDevReloadListener(final ReactContextCreator reactContextCreator) {
44
+        new JsDevReloadListenerReplacer(reactInstanceManager, new JsDevReloadListenerReplacer.Listener() {
45
+            @Override
46
+            public void onJsDevReload() {
47
+                reactContextCreator.onJsDevReload();
48
+            }
49
+        }).replace();
50
+    }
51
+
52
+    private ReactInstanceManager createReactInstanceManager(final ReactContextCreator reactContextCreator) {
53
+        ReactInstanceManager.Builder builder = ReactInstanceManager.builder()
54
+                .setApplication(reactContextCreator.getApplication())
55
+                .setJSMainModuleName("index.android")
56
+                .setBundleAssetName("index.android.bundle")
57
+                .setUseDeveloperSupport(BuildConfig.DEBUG)
58
+                .setInitialLifecycleState(LifecycleState.BEFORE_RESUME);
59
+
60
+        for (ReactPackage reactPackage : reactContextCreator.createReactPackages()) {
61
+            builder.addPackage(reactPackage);
62
+        }
63
+
64
+        return builder.build();
65
+    }
66
+}

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java 查看文件

8
 
8
 
9
 import com.facebook.react.ReactInstanceManager;
9
 import com.facebook.react.ReactInstanceManager;
10
 import com.reactnativenavigation.activities.BaseReactActivity;
10
 import com.reactnativenavigation.activities.BaseReactActivity;
11
-import com.reactnativenavigation.core.RctManager;
12
 import com.reactnativenavigation.core.objects.Screen;
11
 import com.reactnativenavigation.core.objects.Screen;
13
 
12
 
14
 import java.util.Stack;
13
 import java.util.Stack;