diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java index 6922aa817418d4..7152a19ab56300 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/BridgelessDevSupportManager.java @@ -121,7 +121,7 @@ public void toggleElementInspector() { @androidx.annotation.Nullable @Override public Activity getCurrentActivity() { - return reactHost.getCurrentActivity(); + return reactHost.getLastUsedActivity(); } @Override diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHostImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHostImpl.java index b5b8ebb474934b..b59ffafeffcd23 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHostImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHostImpl.java @@ -117,6 +117,8 @@ public class ReactHostImpl implements ReactHost { new BridgelessAtomicRef<>(); private final AtomicReference mActivity = new AtomicReference<>(); + private final AtomicReference> mLastUsedActivity = + new AtomicReference<>(new WeakReference<>(null)); private final BridgelessReactStateTracker mBridgelessReactStateTracker = new BridgelessReactStateTracker(DEV); private final ReactLifecycleStateManager mReactLifecycleStateManager = @@ -504,8 +506,20 @@ private MemoryPressureListener createMemoryPressureListener(ReactInstance reactI return mActivity.get(); } + @Nullable + /* package */ Activity getLastUsedActivity() { + @Nullable WeakReference 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)); + } } /**