Skip to content

Commit 7b40c8e

Browse files
Support onWindowFocusChange in Bridgeless (#43398)
Summary: Pull Request resolved: #43398 Implement onWindowFocusChange in Bridgeless by adding it to the ReactHostImpl Changelog: [Android][Breaking] Implement onWindowFocusChange in Bridgeless Reviewed By: javache Differential Revision: D54670119 fbshipit-source-id: 71f560e5a3bf0e853ac06955e67b8035f1ec0468
1 parent 9af98cc commit 7b40c8e

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public class com/facebook/react/ReactDelegate {
153153
public fun onHostDestroy ()V
154154
public fun onHostPause ()V
155155
public fun onHostResume ()V
156+
public fun onWindowFocusChanged (Z)V
156157
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
157158
}
158159

@@ -203,6 +204,7 @@ public abstract interface class com/facebook/react/ReactHost {
203204
public abstract fun onHostPause (Landroid/app/Activity;)V
204205
public abstract fun onHostResume (Landroid/app/Activity;)V
205206
public abstract fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
207+
public abstract fun onWindowFocusChange (Z)V
206208
public abstract fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
207209
public abstract fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
208210
public abstract fun setJsEngineResolutionAlgorithm (Lcom/facebook/react/JSEngineResolutionAlgorithm;)V
@@ -3641,6 +3643,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
36413643
public fun onHostPause (Landroid/app/Activity;)V
36423644
public fun onHostResume (Landroid/app/Activity;)V
36433645
public fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
3646+
public fun onWindowFocusChange (Z)V
36443647
public fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
36453648
public fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
36463649
public fun removeReactInstanceEventListener (Lcom/facebook/react/ReactInstanceEventListener;)V

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,7 @@ public boolean onNewIntent(Intent intent) {
195195
}
196196

197197
public void onWindowFocusChanged(boolean hasFocus) {
198-
if (ReactFeatureFlags.enableBridgelessArchitecture) {
199-
// TODO T156475655: support onWindowFocusChanged
200-
} else {
201-
if (getReactNativeHost().hasInstance()) {
202-
getReactNativeHost().getReactInstanceManager().onWindowFocusChange(hasFocus);
203-
}
204-
}
198+
mReactDelegate.onWindowFocusChanged(hasFocus);
205199
}
206200

207201
public void onConfigurationChanged(Configuration newConfig) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactDelegate.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ public void onActivityResult(
148148
}
149149
}
150150

151+
public void onWindowFocusChanged(boolean hasFocus) {
152+
if (ReactFeatureFlags.enableBridgelessArchitecture) {
153+
mReactHost.onWindowFocusChange(hasFocus);
154+
} else {
155+
if (getReactNativeHost().hasInstance()) {
156+
getReactNativeHost().getReactInstanceManager().onWindowFocusChange(hasFocus);
157+
}
158+
}
159+
}
160+
151161
public void loadApp() {
152162
loadApp(mMainComponentName);
153163
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactHost.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public interface ReactHost {
120120
data: Intent?,
121121
)
122122

123+
/* To be called when focus has changed for the hosting window. */
124+
public fun onWindowFocusChange(hasFocus: Boolean)
125+
123126
public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)
124127

125128
public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,23 @@ public void onActivityResult(
650650
"Tried to access onActivityResult while context is not ready"));
651651
}
652652

653+
/* To be called when focus has changed for the hosting window. */
654+
@ThreadConfined(UI)
655+
@Override
656+
public void onWindowFocusChange(boolean hasFocus) {
657+
final String method = "onWindowFocusChange(hasFocus = \"" + hasFocus + "\")";
658+
log(method);
659+
660+
ReactContext currentContext = getCurrentReactContext();
661+
if (currentContext != null) {
662+
currentContext.onWindowFocusChange(hasFocus);
663+
}
664+
ReactSoftExceptionLogger.logSoftException(
665+
TAG,
666+
new ReactNoCrashSoftException(
667+
"Tried to access onWindowFocusChange while context is not ready"));
668+
}
669+
653670
@Nullable
654671
JavaScriptContextHolder getJavaScriptContextHolder() {
655672
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();

0 commit comments

Comments
 (0)