|
7 | 7 | import android.os.Looper; |
8 | 8 | import android.view.Choreographer; |
9 | 9 | import android.view.View; |
| 10 | +import android.view.ViewGroup; |
10 | 11 |
|
11 | 12 | import androidx.annotation.OptIn; |
12 | 13 |
|
@@ -268,6 +269,7 @@ private void loadBundle() { |
268 | 269 | @Override |
269 | 270 | public void run() { |
270 | 271 | try { |
| 272 | + resetReactRootViews(instanceManager); |
271 | 273 | instanceManager.recreateReactContextInBackground(); |
272 | 274 | mCodePush.initializeUpdateAfterRestart(); |
273 | 275 | } catch (Exception e) { |
@@ -315,6 +317,26 @@ private void clearLifecycleEventListener() { |
315 | 317 | } |
316 | 318 | } |
317 | 319 |
|
| 320 | + private void resetReactRootViews(ReactInstanceManager instanceManager) throws NoSuchFieldException, IllegalAccessException { |
| 321 | + Field mAttachedReactRootsField = instanceManager.getClass().getDeclaredField("mAttachedReactRoots"); |
| 322 | + mAttachedReactRootsField.setAccessible(true); |
| 323 | + java.util.Set<?> mAttachedReactRoots = (java.util.Set<?>) mAttachedReactRootsField.get(instanceManager); |
| 324 | + if (mAttachedReactRoots != null) { |
| 325 | + for (Object reactRoot : mAttachedReactRoots) { |
| 326 | + try { |
| 327 | + Method getRootViewGroupMethod = reactRoot.getClass().getMethod("getRootViewGroup"); |
| 328 | + ViewGroup rootViewGroup = (ViewGroup) getRootViewGroupMethod.invoke(reactRoot); |
| 329 | + if (rootViewGroup != null) { |
| 330 | + rootViewGroup.removeAllViews(); |
| 331 | + rootViewGroup.setId(View.NO_ID); |
| 332 | + } |
| 333 | + } catch (Exception e) { |
| 334 | + CodePushUtils.log("Failed to reset root view: " + e.getMessage()); |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | + |
318 | 340 | // Use reflection to find the ReactInstanceManager. See #556 for a proposal for a less brittle way to approach this. |
319 | 341 | private ReactInstanceManager resolveInstanceManager() throws NoSuchFieldException, IllegalAccessException { |
320 | 342 | ReactInstanceManager instanceManager = CodePush.getReactInstanceManager(); |
|
0 commit comments