Fix: RedBoxes don't show up after teardowns (#38997)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38997

After React Native gets destroyed (e.g: via an exception), the ReactHost resets its current activity.

## Problem
React Native can display RedBoxes after React Native destruction (e.g: in the case of an exception).

Displaying RedBoxes requires the current activity, which gets nullified. So, the RedBox might not show up after destruction.

## Changes
This diff makes ReactHost keep a track of its last non-null activity in a WeakRef.
Then, the DevMenu just uses the last non-null activity to display RedBoxes (and everything else).

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D48076893

fbshipit-source-id: 6baddf8f329c03efe92f93ef1bfc6eb9569c1b38
This commit is contained in:
Ramanpreet Nara
2023-08-21 12:43:42 -07:00
committed by Facebook GitHub Bot
parent aec22574cc
commit 1f0094e40a
2 changed files with 15 additions and 1 deletions
@@ -121,7 +121,7 @@ class BridgelessDevSupportManager extends DevSupportManagerBase {
@androidx.annotation.Nullable
@Override
public Activity getCurrentActivity() {
return reactHost.getCurrentActivity();
return reactHost.getLastUsedActivity();
}
@Override
@@ -117,6 +117,8 @@ public class ReactHostImpl implements ReactHost {
new BridgelessAtomicRef<>();
private final AtomicReference<Activity> mActivity = new AtomicReference<>();
private final AtomicReference<WeakReference<Activity>> mLastUsedActivity =
new AtomicReference<>(new WeakReference<>(null));
private final BridgelessReactStateTracker mBridgelessReactStateTracker =
new BridgelessReactStateTracker(DEV);
private final ReactLifecycleStateManager mReactLifecycleStateManager =
@@ -504,8 +506,20 @@ public class ReactHostImpl implements ReactHost {
return mActivity.get();
}
@Nullable
/* package */ Activity getLastUsedActivity() {
@Nullable WeakReference<Activity> lastUsedActivityWeakRef = mLastUsedActivity.get();
if (lastUsedActivityWeakRef != null) {
return lastUsedActivityWeakRef.get();
}
return null;
}
private void setCurrentActivity(@Nullable Activity activity) {
mActivity.set(activity);
if (activity != null) {
mLastUsedActivity.set(new WeakReference<>(activity));
}
}
/**