5
5
import android .os .Handler ;
6
6
import android .os .Looper ;
7
7
import android .provider .Settings ;
8
+ import android .view .View ;
8
9
9
10
import com .facebook .react .ReactApplication ;
10
11
import com .facebook .react .ReactInstanceManager ;
12
+ import com .facebook .react .ReactRootView ;
11
13
import com .facebook .react .bridge .Arguments ;
12
14
import com .facebook .react .bridge .LifecycleEventListener ;
13
15
import com .facebook .react .bridge .Promise ;
29
31
import java .lang .reflect .Method ;
30
32
import java .util .Date ;
31
33
import java .util .HashMap ;
34
+ import java .util .List ;
32
35
import java .util .Map ;
33
36
34
37
public class CodePushNativeModule extends ReactContextBaseJavaModule {
@@ -101,7 +104,7 @@ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBu
101
104
Class <?> jsBundleLoaderClass = Class .forName ("com.facebook.react.cxxbridge.JSBundleLoader" );
102
105
Method createFileLoaderMethod = null ;
103
106
String createFileLoaderMethodName = latestJSBundleFile .toLowerCase ().startsWith ("assets://" )
104
- ? "createAssetLoader" : "createFileLoader" ;
107
+ ? "createAssetLoader" : "createFileLoader" ;
105
108
106
109
Method [] methods = jsBundleLoaderClass .getDeclaredMethods ();
107
110
for (Method method : methods ) {
@@ -157,6 +160,11 @@ private void loadBundle() {
157
160
@ Override
158
161
public void run () {
159
162
try {
163
+ // This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533
164
+ // resetReactRootViews allows to call recreateReactContextInBackground without any exceptions
165
+ // This fix also relates to https://github.com/Microsoft/react-native-code-push/issues/878
166
+ resetReactRootViews (instanceManager );
167
+
160
168
instanceManager .recreateReactContextInBackground ();
161
169
mCodePush .initializeUpdateAfterRestart ();
162
170
} catch (Exception e ) {
@@ -174,6 +182,17 @@ public void run() {
174
182
}
175
183
}
176
184
185
+ private void resetReactRootViews (ReactInstanceManager instanceManager ) throws NoSuchFieldException , IllegalAccessException {
186
+ Field mAttachedRootViewsField = instanceManager .getClass ().getDeclaredField ("mAttachedRootViews" );
187
+ mAttachedRootViewsField .setAccessible (true );
188
+ List <ReactRootView > mAttachedRootViews = (List <ReactRootView >)mAttachedRootViewsField .get (instanceManager );
189
+ for (ReactRootView reactRootView : mAttachedRootViews ) {
190
+ reactRootView .removeAllViews ();
191
+ reactRootView .setId (View .NO_ID );
192
+ }
193
+ mAttachedRootViewsField .set (instanceManager , mAttachedRootViews );
194
+ }
195
+
177
196
private void clearLifecycleEventListener () {
178
197
// Remove LifecycleEventListener to prevent infinite restart loop
179
198
if (mLifecycleEventListener != null ) {
0 commit comments