Skip to content

Commit

Permalink
Refactor: Use get/setCurrentActivity in ReactHost (#38998)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #38998

Instead of using the mActivity reference directly, let's just use getCurrentActivity() and setCurrentActivity().

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D48076896

fbshipit-source-id: d988deaf8778bf0e3726d13f96e9e6e480d8fdc0
  • Loading branch information
RSNara authored and facebook-github-bot committed Aug 21, 2023
1 parent 75c5d74 commit aec2257
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ public void onHostResume(final @Nullable Activity activity) {
final String method = "onHostResume(activity)";
log(method);

mActivity.set(activity);
setCurrentActivity(activity);
ReactContext currentContext = getCurrentReactContext();

// TODO(T137233065): Enable DevSupportManager here
mReactLifecycleStateManager.moveToOnHostResume(currentContext, mActivity.get());
mReactLifecycleStateManager.moveToOnHostResume(currentContext, getCurrentActivity());
}

@ThreadConfined(UI)
Expand All @@ -291,7 +291,7 @@ public void onHostPause(final @Nullable Activity activity) {

ReactContext currentContext = getCurrentReactContext();

Activity currentActivity = mActivity.get();
Activity currentActivity = getCurrentActivity();
if (currentActivity != null) {
String currentActivityClass = currentActivity.getClass().getSimpleName();
String activityClass = activity == null ? "null" : activity.getClass().getSimpleName();
Expand Down Expand Up @@ -321,7 +321,7 @@ public void onHostPause() {

// TODO(T137233065): Disable DevSupportManager here
mDefaultHardwareBackBtnHandler = null;
mReactLifecycleStateManager.moveToOnHostPause(currentContext, mActivity.get());
mReactLifecycleStateManager.moveToOnHostPause(currentContext, getCurrentActivity());
}

/** To be called when the host activity is destroyed. */
Expand All @@ -341,7 +341,7 @@ public void onHostDestroy(@Nullable Activity activity) {
final String method = "onHostDestroy(activity)";
log(method);

Activity currentActivity = mActivity.get();
Activity currentActivity = getCurrentActivity();

// TODO(T137233065): Disable DevSupportManager here
if (currentActivity == activity) {
Expand Down Expand Up @@ -504,6 +504,10 @@ private MemoryPressureListener createMemoryPressureListener(ReactInstance reactI
return mActivity.get();
}

private void setCurrentActivity(@Nullable Activity activity) {
mActivity.set(activity);
}

/**
* Get the {@link EventDispatcher} from the {@link FabricUIManager}. This always returns an
* EventDispatcher, even if the instance isn't alive; in that case, it returns a {@link
Expand Down Expand Up @@ -726,7 +730,7 @@ private Task<Void> newStart() {
@ThreadConfined(UI)
private void moveToHostDestroy(@Nullable ReactContext currentContext) {
mReactLifecycleStateManager.moveToOnHostDestroy(currentContext);
mActivity.set(null);
setCurrentActivity(null);
}

private void raiseSoftException(String method, String message) {
Expand Down Expand Up @@ -946,14 +950,15 @@ class Result {
* screen in the past, or (2) We must be on a React Native screen.
*/
if (isReloading && !isManagerResumed) {
mReactLifecycleStateManager.moveToOnHostResume(reactContext, mActivity.get());
mReactLifecycleStateManager.moveToOnHostResume(
reactContext, getCurrentActivity());
} else {
/**
* Call ReactContext.onHostResume() only when already in the resumed state
* which aligns with the bridge https://fburl.com/diffusion/2qhxmudv.
*/
mReactLifecycleStateManager.resumeReactContextIfHostResumed(
reactContext, mActivity.get());
reactContext, getCurrentActivity());
}

ReactInstanceEventListener[] listeners =
Expand Down Expand Up @@ -1033,7 +1038,7 @@ private Task<ReactInstance> oldGetOrCreateReactInstanceTask() {
aligns with the bridge https://fburl.com/diffusion/2qhxmudv.
*/
mReactLifecycleStateManager.resumeReactContextIfHostResumed(
reactContext, mActivity.get());
reactContext, getCurrentActivity());

ReactInstanceEventListener[] listeners =
new ReactInstanceEventListener[mReactInstanceEventListeners.size()];
Expand Down Expand Up @@ -1410,7 +1415,7 @@ private Task<Void> newGetOrCreateDestroyTask(final String reason, @Nullable Exce
}

// Reset current activity
mActivity.set(null);
setCurrentActivity(null);

// Clear ResourceIdleDrawableIdMap
ResourceDrawableIdHelper.getInstance().clear();
Expand Down

0 comments on commit aec2257

Please sign in to comment.