Skip to content

Commit

Permalink
Back out "Add onUserLeaveHint support to ReactActivityDelegate" (fa…
Browse files Browse the repository at this point in the history
…cebook#42791)

Summary:
Pull Request resolved: facebook#42791

Original commit changeset: 491fc062421d

Original Phabricator Diff: D53279501

Reviewed By: mdvacca

Differential Revision: D53321360

fbshipit-source-id: 36ce929250077a9c2919ba7f01d937dc09986ec8
  • Loading branch information
cortinico authored and facebook-github-bot committed Feb 1, 2024
1 parent 107f2f9 commit 78f5002
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 112 deletions.
8 changes: 0 additions & 8 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/
protected fun onPause ()V
public fun onRequestPermissionsResult (I[Ljava/lang/String;[I)V
protected fun onResume ()V
public fun onUserLeaveHint ()V
public fun onWindowFocusChanged (Z)V
public fun requestPermissions ([Ljava/lang/String;ILcom/facebook/react/modules/core/PermissionListener;)V
}
Expand Down Expand Up @@ -131,7 +130,6 @@ public class com/facebook/react/ReactActivityDelegate {
protected fun onPause ()V
public fun onRequestPermissionsResult (I[Ljava/lang/String;[I)V
protected fun onResume ()V
protected fun onUserLeaveHint ()V
public fun onWindowFocusChanged (Z)V
public fun requestPermissions ([Ljava/lang/String;ILcom/facebook/react/modules/core/PermissionListener;)V
}
Expand All @@ -156,7 +154,6 @@ public class com/facebook/react/ReactDelegate {
public fun onHostDestroy ()V
public fun onHostPause ()V
public fun onHostResume ()V
public fun onUserLeaveHint ()V
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
}

Expand Down Expand Up @@ -202,7 +199,6 @@ public abstract interface class com/facebook/react/ReactHost {
public abstract fun onBackPressed ()Z
public abstract fun onHostDestroy ()V
public abstract fun onHostDestroy (Landroid/app/Activity;)V
public abstract fun onHostLeaveHint (Landroid/app/Activity;)V
public abstract fun onHostPause ()V
public abstract fun onHostPause (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;)V
Expand Down Expand Up @@ -244,7 +240,6 @@ public class com/facebook/react/ReactInstanceManager {
public fun onHostResume (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public fun onNewIntent (Landroid/content/Intent;)V
public fun onUserLeaveHint (Landroid/app/Activity;)V
public fun onWindowFocusChange (Z)V
public fun recreateReactContextInBackground ()V
public fun removeReactInstanceEventListener (Lcom/facebook/react/ReactInstanceEventListener;)V
Expand Down Expand Up @@ -479,7 +474,6 @@ public class com/facebook/react/animated/NativeAnimatedNodesManager : com/facebo
public abstract interface class com/facebook/react/bridge/ActivityEventListener {
public abstract fun onActivityResult (Landroid/app/Activity;IILandroid/content/Intent;)V
public abstract fun onNewIntent (Landroid/content/Intent;)V
public fun onUserLeaveHint (Landroid/app/Activity;)V
}

public class com/facebook/react/bridge/Arguments {
Expand Down Expand Up @@ -1098,7 +1092,6 @@ public class com/facebook/react/bridge/ReactContext : android/content/ContextWra
public fun onHostPause ()V
public fun onHostResume (Landroid/app/Activity;)V
public fun onNewIntent (Landroid/app/Activity;Landroid/content/Intent;)V
public fun onUserLeaveHint (Landroid/app/Activity;)V
public fun onWindowFocusChange (Z)V
public fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
public fun removeActivityEventListener (Lcom/facebook/react/bridge/ActivityEventListener;)V
Expand Down Expand Up @@ -3628,7 +3621,6 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
public fun onBackPressed ()Z
public fun onHostDestroy ()V
public fun onHostDestroy (Landroid/app/Activity;)V
public fun onHostLeaveHint (Landroid/app/Activity;)V
public fun onHostPause ()V
public fun onHostPause (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ public void onNewIntent(Intent intent) {
}
}

@Override
public void onUserLeaveHint() {
super.onUserLeaveHint();
mDelegate.onUserLeaveHint();
}

@Override
public void requestPermissions(
String[] permissions, int requestCode, PermissionListener listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ protected void loadApp(String appKey) {
getPlainActivity().setContentView(mReactDelegate.getReactRootView());
}

protected void onUserLeaveHint() {
mReactDelegate.onUserLeaveHint();
}

protected void onPause() {
mReactDelegate.onHostPause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ public void onHostResume() {
}
}

public void onUserLeaveHint() {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactHost.onHostLeaveHint(mActivity);
} else {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onUserLeaveHint(mActivity);
}
}
}

public void onHostPause() {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactHost.onHostPause(mActivity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ interface ReactHost {
/** To be called when the host activity is resumed. */
fun onHostResume(activity: Activity?)

/**
* To be called when the host activity is about to go into the background as the result of user
* choice.
*/
fun onHostLeaveHint(activity: Activity?)

/** To be called when the host activity is paused. */
fun onHostPause(activity: Activity?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,38 +585,6 @@ public void onHostPause() {
moveToBeforeResumeLifecycleState();
}

/**
* This method should be called from {@link Activity#onUserLeaveHint()}. It notifies all listening
* modules that the user is about to leave the activity. The passed Activity is has to be the
* current Activity.
*
* @param activity the activity being backgrounded as a result of user action
*/
@ThreadConfined(UI)
public void onUserLeaveHint(@Nullable Activity activity) {
if (mRequireActivity) {
Assertions.assertCondition(mCurrentActivity != null);
}

if (mCurrentActivity != null) {
Assertions.assertCondition(
activity == mCurrentActivity,
"Called onUserLeaveHint on an activity that is not the current activity, this is incorrect! "
+ "Current activity: "
+ mCurrentActivity.getClass().getSimpleName()
+ " "
+ "Leaving activity: "
+ activity.getClass().getSimpleName());

UiThreadUtil.assertOnUiThread();

ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
currentContext.onUserLeaveHint(activity);
}
}
}

/**
* Call this from {@link Activity#onPause()}. This notifies any listening modules so they can do
* any necessary cleanup. The passed Activity is the current Activity being paused. This will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ public interface ActivityEventListener {

/** Called when a new intent is passed to the activity */
void onNewIntent(Intent intent);

/** Called when host activity receives an {@link Activity#onUserLeaveHint()} call. */
default void onUserLeaveHint(Activity activity) {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,6 @@ public void onHostResume(@Nullable Activity activity) {
ReactMarker.logMarker(ReactMarkerConstants.ON_HOST_RESUME_END);
}

@ThreadConfined(UI)
public void onUserLeaveHint(@Nullable Activity activity) {
for (ActivityEventListener listener : mActivityEventListeners) {
try {
listener.onUserLeaveHint(activity);
} catch (RuntimeException e) {
handleException(e);
}
}
}

@ThreadConfined(UI)
public void onNewIntent(@Nullable Activity activity, Intent intent) {
UiThreadUtil.assertOnUiThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public void onHostResume(
@ThreadConfined(UI)
@Override
public void onHostResume(final @Nullable Activity activity) {
log("onHostResume(activity)");
final String method = "onHostResume(activity)";
log(method);

setCurrentActivity(activity);
ReactContext currentContext = getCurrentReactContext();
Expand All @@ -285,21 +286,11 @@ public void onHostResume(final @Nullable Activity activity) {
mReactLifecycleStateManager.moveToOnHostResume(currentContext, getCurrentActivity());
}

@ThreadConfined(UI)
@Override
public void onHostLeaveHint(final @Nullable Activity activity) {
log("onUserLeaveHint(activity)");

ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
currentContext.onUserLeaveHint(activity);
}
}

@ThreadConfined(UI)
@Override
public void onHostPause(final @Nullable Activity activity) {
log("onHostPause(activity)");
final String method = "onHostPause(activity)";
log(method);

ReactContext currentContext = getCurrentReactContext();

Expand All @@ -326,7 +317,8 @@ public void onHostPause(final @Nullable Activity activity) {
@ThreadConfined(UI)
@Override
public void onHostPause() {
log("onHostPause()");
final String method = "onHostPause()";
log(method);

ReactContext currentContext = getCurrentReactContext();

Expand All @@ -339,7 +331,8 @@ public void onHostPause() {
@ThreadConfined(UI)
@Override
public void onHostDestroy() {
log("onHostDestroy()");
final String method = "onHostDestroy()";
log(method);

// TODO(T137233065): Disable DevSupportManager here
moveToHostDestroy(getCurrentReactContext());
Expand All @@ -348,7 +341,8 @@ public void onHostDestroy() {
@ThreadConfined(UI)
@Override
public void onHostDestroy(@Nullable Activity activity) {
log("onHostDestroy(activity)");
final String method = "onHostDestroy(activity)";
log(method);

Activity currentActivity = getCurrentActivity();

Expand Down Expand Up @@ -481,11 +475,12 @@ public TaskInterface<Void> reload(String reason) {
*/
@Override
public TaskInterface<Void> destroy(String reason, @Nullable Exception ex) {
final String method = "destroy()";
return Task.call(
() -> {
if (mReloadTask != null) {
log(
"destroy()",
method,
"Reloading React Native. Waiting for reload to finish before destroying React Native.");
return mReloadTask.continueWithTask(
task -> getOrCreateDestroyTask(reason, ex), mBGExecutor);
Expand Down Expand Up @@ -646,23 +641,26 @@ DefaultHardwareBackBtnHandler getDefaultBackButtonHandler() {
*/
/* package */ Task<Boolean> callFunctionOnModule(
final String moduleName, final String methodName, final NativeArray args) {
final String method = "callFunctionOnModule(\"" + moduleName + "\", \"" + methodName + "\")";
return callWithExistingReactInstance(
"callFunctionOnModule(\"" + moduleName + "\", \"" + methodName + "\")",
method,
reactInstance -> {
reactInstance.callFunctionOnModule(moduleName, methodName, args);
});
}

/* package */ void attachSurface(ReactSurfaceImpl surface) {
log("attachSurface(surfaceId = " + surface.getSurfaceID() + ")");
final String method = "attachSurface(surfaceId = " + surface.getSurfaceID() + ")";
log(method);

synchronized (mAttachedSurfaces) {
mAttachedSurfaces.add(surface);
}
}

/* package */ void detachSurface(ReactSurfaceImpl surface) {
log("detachSurface(surfaceId = " + surface.getSurfaceID() + ")");
final String method = "detachSurface(surfaceId = " + surface.getSurfaceID() + ")";
log(method);

synchronized (mAttachedSurfaces) {
mAttachedSurfaces.remove(surface);
Expand Down Expand Up @@ -709,8 +707,9 @@ public void removeBeforeDestroyListener(@NonNull Function0<Unit> onBeforeDestroy

@ThreadConfined("ReactHost")
private Task<Void> getOrCreateStartTask() {
final String method = "getOrCreateStartTask()";
if (mStartTask == null) {
log("getOrCreateStartTask()", "Schedule");
log(method, "Schedule");
mStartTask =
waitThenCallGetOrCreateReactInstanceTask()
.continueWithTask(
Expand Down Expand Up @@ -757,16 +756,15 @@ private void raiseSoftException(String method, String message, @Nullable Throwab

private Task<Boolean> callWithExistingReactInstance(
final String callingMethod, final VeniceThenable<ReactInstance> continuation) {
final String method = "callWithExistingReactInstance(" + callingMethod + ")";

return mReactInstanceTaskRef
.get()
.onSuccess(
task -> {
final ReactInstance reactInstance = task.getResult();
if (reactInstance == null) {
raiseSoftException(
"callWithExistingReactInstance(" + callingMethod + ")",
"Execute: ReactInstance null. Dropping work.");
raiseSoftException(method, "Execute: ReactInstance null. Dropping work.");
return FALSE;
}

Expand All @@ -778,16 +776,15 @@ private Task<Boolean> callWithExistingReactInstance(

private Task<Void> callAfterGetOrCreateReactInstance(
final String callingMethod, final VeniceThenable<ReactInstance> runnable) {
final String method = "callAfterGetOrCreateReactInstance(" + callingMethod + ")";

return getOrCreateReactInstance()
.onSuccess(
(Continuation<ReactInstance, Void>)
task -> {
final ReactInstance reactInstance = task.getResult();
if (reactInstance == null) {
raiseSoftException(
"callAfterGetOrCreateReactInstance(" + callingMethod + ")",
"Execute: ReactInstance is null");
raiseSoftException(method, "Execute: ReactInstance is null");
return null;
}

Expand All @@ -806,9 +803,10 @@ private Task<Void> callAfterGetOrCreateReactInstance(
}

private BridgelessReactContext getOrCreateReactContext() {
final String method = "getOrCreateReactContext()";
return mBridgelessReactContextRef.getOrCreate(
() -> {
log("getOrCreateReactContext()", "Creating BridgelessReactContext");
log(method, "Creating BridgelessReactContext");
return new BridgelessReactContext(mContext, ReactHostImpl.this);
});
}
Expand Down Expand Up @@ -931,7 +929,7 @@ class Result {
final boolean isManagerResumed =
mReactLifecycleStateManager.getLifecycleState() == LifecycleState.RESUMED;

/*
/**
* ReactContext.onHostResume() should only be called when the user navigates to
* the first React Native screen.
*
Expand All @@ -955,7 +953,7 @@ class Result {
mReactLifecycleStateManager.moveToOnHostResume(
reactContext, getCurrentActivity());
} else {
/*
/**
* Call ReactContext.onHostResume() only when already in the resumed state
* which aligns with the bridge https://fburl.com/diffusion/2qhxmudv.
*/
Expand All @@ -981,7 +979,8 @@ class Result {
}

private Task<JSBundleLoader> getJsBundleLoader() {
log("getJSBundleLoader()");
final String method = "getJSBundleLoader()";
log(method);

if (DEV && mAllowPackagerServerAccess) {
return isMetroRunning()
Expand Down Expand Up @@ -1013,7 +1012,7 @@ private Task<JSBundleLoader> getJsBundleLoader() {

private Task<Boolean> isMetroRunning() {
final String method = "isMetroRunning()";
log("isMetroRunning()");
log(method);

final TaskCompletionSource<Boolean> taskCompletionSource = new TaskCompletionSource<>();
final DevSupportManager asyncDevSupportManager = getDevSupportManager();
Expand Down

0 comments on commit 78f5002

Please sign in to comment.