You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*** Recreate the react application and context. This should be called if configuration has* changed or the developer has requested the app to be reloaded. It should only be called after* an initial call to createReactContextInBackground.** Called from UI thread.*/@ThreadConfined(UI)
publicvoidrecreateReactContextInBackground() {
Assertions.assertCondition(
mHasStartedCreatingInitialContext,
"recreateReactContextInBackground should only be called after the initial " +
"createReactContextInBackground call.");
recreateReactContextInBackgroundInner();
}
我们知道react native app可以动态下发js bundle实现热更新,但是一般需要用户重启app才能应用更新。
reload
RN内置的开发菜单有一个reload功能:
实际上,React Native是允许我们在不重启App的情况下刷新应用。
IOS
在IOS中,Reload功能最终是通过RCTBridge的reload方法来刷新应用:
而在setUp中,RCTBridge可以通过一个delegate对象来更新bundleURL:
所以,我们所要做的就是改变一下RCTBridge的创建方式:
我们把AppDelegate作为RCTBridge代理对象,然后实现一个sourceURLForBridge方法:
这样bridge就可以在reload时更新bundleURL,实现应用刷新了。
Android
在Android中,Reload功能最终是通过ReactInstanceManager的recreateReactContextInBackground方法来刷新应用:
但是这个方法是private的,不对外开放。
ReactInstanceManager同时提供了一个不带参数的recreateReactContextInBackground实现:
这个方法是public的,可以被外部调用。
不过在调用之前,我们需要先通过代码反射技术更新一下mBundleLoader:
然后就可以在主线程中调用recreateReactContextInBackground:
这样就可以实现应用刷新了。
The text was updated successfully, but these errors were encountered: