Skip to content

Commit

Permalink
Fix: RedBoxes don't always show up after React Native destroys
Browse files Browse the repository at this point in the history
Summary:
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]

Differential Revision: D48076893

fbshipit-source-id: 529973647e92a2e2e9fde4da3ce51df14b14575c
  • Loading branch information
RSNara authored and facebook-github-bot committed Aug 14, 2023
1 parent aea953c commit 9c1680d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void toggleElementInspector() {
@androidx.annotation.Nullable
@Override
public Activity getCurrentActivity() {
return reactHost.getCurrentActivity();
return reactHost.getLastUsedActivity();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,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 =
Expand Down Expand Up @@ -503,8 +505,16 @@ private MemoryPressureListener createMemoryPressureListener(ReactInstance reactI
return mActivity.get();
}

@Nullable
/* package */ Activity getLastUsedActivity() {
return mLastUsedActivity.get().get();
}

private void setCurrentActivity(@Nullable Activity activity) {
mActivity.set(activity);
if (activity != null) {
mLastUsedActivity.set(new WeakReference<>(activity));
}
}

/**
Expand Down

0 comments on commit 9c1680d

Please sign in to comment.