Skip to content

Commit

Permalink
Support onWindowFocusChange in Bridgeless (#43398)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arushikesarwani94 authored and facebook-github-bot committed Mar 12, 2024
1 parent 9af98cc commit 7b40c8e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Expand Up @@ -153,6 +153,7 @@ public class com/facebook/react/ReactDelegate {
public fun onHostDestroy ()V
public fun onHostPause ()V
public fun onHostResume ()V
public fun onWindowFocusChanged (Z)V
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
}

Expand Down Expand Up @@ -203,6 +204,7 @@ public abstract interface class com/facebook/react/ReactHost {
public abstract fun onHostPause (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public abstract fun onWindowFocusChange (Z)V
public abstract fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
public abstract fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
public abstract fun setJsEngineResolutionAlgorithm (Lcom/facebook/react/JSEngineResolutionAlgorithm;)V
Expand Down Expand Up @@ -3641,6 +3643,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
public fun onHostPause (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public fun onWindowFocusChange (Z)V
public fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
public fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
public fun removeReactInstanceEventListener (Lcom/facebook/react/ReactInstanceEventListener;)V
Expand Down
Expand Up @@ -195,13 +195,7 @@ public boolean onNewIntent(Intent intent) {
}

public void onWindowFocusChanged(boolean hasFocus) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
// TODO T156475655: support onWindowFocusChanged
} else {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onWindowFocusChange(hasFocus);
}
}
mReactDelegate.onWindowFocusChanged(hasFocus);
}

public void onConfigurationChanged(Configuration newConfig) {
Expand Down
Expand Up @@ -148,6 +148,16 @@ public void onActivityResult(
}
}

public void onWindowFocusChanged(boolean hasFocus) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactHost.onWindowFocusChange(hasFocus);
} else {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onWindowFocusChange(hasFocus);
}
}
}

public void loadApp() {
loadApp(mMainComponentName);
}
Expand Down
Expand Up @@ -120,6 +120,9 @@ public interface ReactHost {
data: Intent?,
)

/* To be called when focus has changed for the hosting window. */
public fun onWindowFocusChange(hasFocus: Boolean)

public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)

public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)
Expand Down
Expand Up @@ -650,6 +650,23 @@ public void onActivityResult(
"Tried to access onActivityResult while context is not ready"));
}

/* To be called when focus has changed for the hosting window. */
@ThreadConfined(UI)
@Override
public void onWindowFocusChange(boolean hasFocus) {
final String method = "onWindowFocusChange(hasFocus = \"" + hasFocus + "\")";
log(method);

ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
currentContext.onWindowFocusChange(hasFocus);
}
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Tried to access onWindowFocusChange while context is not ready"));
}

@Nullable
JavaScriptContextHolder getJavaScriptContextHolder() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
Expand Down

0 comments on commit 7b40c8e

Please sign in to comment.