From fb550e9e8405870167dea9bd3bc3f50fbbae5ae5 Mon Sep 17 00:00:00 2001 From: "Jerry.Luo" Date: Wed, 12 Jun 2019 05:06:46 -0700 Subject: [PATCH] Check if mCurrentActivity is set according to LifecycleState (#23336) Summary: Issues: Related to #13439 react-native-website:Related to PR [#792](https://github.com/facebook/react-native-website/pull/792) solution: https://github.com/facebook/react-native/issues/13439#issuecomment-400256114 When we integration with Existing Android Apps.and set LifecycleState is `LifecycleState.RESUMED`. It's lead to `mCurrentActivity` is null . At this time , the behave of set `mCurrentActivity ` which is unexpectedly. ## Changelog [Android] [Fixed] - Check if mCurrentActivity is set according to LifecycleState Pull Request resolved: https://github.com/facebook/react-native/pull/23336 Differential Revision: D14298654 Pulled By: cpojer fbshipit-source-id: 5cc17539a51154faeb838349b068d92511946f79 --- .../com/facebook/react/testing/ReactAppTestActivity.java | 8 ++++++++ .../com/facebook/react/ReactInstanceManagerBuilder.java | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java index 85c8ab0ff22..6fdc5431de6 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java @@ -213,6 +213,14 @@ public class ReactAppTestActivity extends FragmentActivity } else { builder.addPackage(new MainReactPackage()); } + /** + * The {@link ReactContext#mCurrentActivity} never to be set if initial lifecycle state is resumed. + * So we should call {@link ReactInstanceManagerBuilder#setCurrentActivity}. + * + * Finally,{@link ReactInstanceManagerBuilder#build()} will create instance of {@link ReactInstanceManager}. + * And also will set {@link ReactContext#mCurrentActivity}. + */ + builder.setCurrentActivity(this); builder .addPackage(new InstanceSpecForTestPackage(spec)) // By not setting a JS module name, we force the bundle to be always loaded from diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index 00f3bb5d003..47fc17b0ee7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -246,6 +246,12 @@ public class ReactInstanceManagerBuilder { mApplication, "Application property has not been set with this builder"); + if (mInitialLifecycleState == LifecycleState.RESUMED) { + Assertions.assertNotNull( + mCurrentActivity, + "Activity needs to be set if initial lifecycle state is resumed"); + } + Assertions.assertCondition( mUseDeveloperSupport || mJSBundleAssetUrl != null || mJSBundleLoader != null, "JS Bundle File or Asset URL has to be provided when dev support is disabled");