Kotlinify ReactLifecycleStateManager (#50685)

Summary:
Implemented ReactLifecycleStateManager.java in Kotlin as part of Kotlin-ifying RN Round 3

## Changelog:

[ANDROID] [CHANGED] - Migrate ReactLifecycleStateManager to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/50685

Test Plan: Run RN-Tester and interacted with Mulitple components(Image, Flatlist, Input ) with both new architecture enabled and disabled

Reviewed By: rshest

Differential Revision: D73003097

Pulled By: cortinico

fbshipit-source-id: 27b90a0b94c17aa42cbb1665ca6fcf06db7cbf96
This commit is contained in:
rohitverma-d11
2025-04-15 07:51:14 -07:00
committed by Facebook GitHub Bot
parent 555ffd82e8
commit 800b12406f
3 changed files with 92 additions and 91 deletions
@@ -13,7 +13,7 @@ import java.util.Collections
internal class BridgelessReactStateTracker(private val shouldTrackStates: Boolean) {
private val states = Collections.synchronizedList(mutableListOf<String>())
protected fun enterState(state: String) {
fun enterState(state: String) {
FLog.w(TAG, state)
if (shouldTrackStates) {
states.add(state)
@@ -1,90 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.runtime;
import static com.facebook.infer.annotation.ThreadConfined.UI;
import android.app.Activity;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.common.LifecycleState;
@Nullsafe(Nullsafe.Mode.LOCAL)
class ReactLifecycleStateManager {
LifecycleState mState = LifecycleState.BEFORE_CREATE;
private final BridgelessReactStateTracker mBridgelessReactStateTracker;
ReactLifecycleStateManager(BridgelessReactStateTracker bridgelessReactStateTracker) {
mBridgelessReactStateTracker = bridgelessReactStateTracker;
}
public LifecycleState getLifecycleState() {
return mState;
}
@ThreadConfined(UI)
public void resumeReactContextIfHostResumed(
final ReactContext currentContext, final @Nullable Activity activity) {
if (mState == LifecycleState.RESUMED) {
mBridgelessReactStateTracker.enterState("ReactContext.onHostResume()");
currentContext.onHostResume(activity);
}
}
@ThreadConfined(UI)
public void moveToOnHostResume(
final @Nullable ReactContext currentContext, final @Nullable Activity activity) {
if (mState == LifecycleState.RESUMED) {
return;
}
if (currentContext != null) {
mBridgelessReactStateTracker.enterState("ReactContext.onHostResume()");
currentContext.onHostResume(activity);
}
mState = LifecycleState.RESUMED;
}
@ThreadConfined(UI)
public void moveToOnHostPause(
final @Nullable ReactContext currentContext, final @Nullable Activity activity) {
if (currentContext != null) {
if (mState == LifecycleState.BEFORE_CREATE) {
// TODO: Investigate if we can remove this transition.
mBridgelessReactStateTracker.enterState("ReactContext.onHostResume()");
currentContext.onHostResume(activity);
mBridgelessReactStateTracker.enterState("ReactContext.onHostPause()");
currentContext.onHostPause();
} else if (mState == LifecycleState.RESUMED) {
mBridgelessReactStateTracker.enterState("ReactContext.onHostPause()");
currentContext.onHostPause();
}
}
mState = LifecycleState.BEFORE_RESUME;
}
@ThreadConfined(UI)
public void moveToOnHostDestroy(final @Nullable ReactContext currentContext) {
if (currentContext != null) {
if (mState == LifecycleState.BEFORE_RESUME) {
mBridgelessReactStateTracker.enterState("ReactContext.onHostDestroy()");
currentContext.onHostDestroy();
} else if (mState == LifecycleState.RESUMED) {
mBridgelessReactStateTracker.enterState("ReactContext.onHostPause()");
currentContext.onHostPause();
mBridgelessReactStateTracker.enterState("ReactContext.onHostDestroy()");
currentContext.onHostDestroy();
}
}
mState = LifecycleState.BEFORE_CREATE;
}
}
@@ -0,0 +1,91 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.runtime
import android.app.Activity
import com.facebook.infer.annotation.ThreadConfined
import com.facebook.infer.annotation.ThreadConfined.UI
import com.facebook.react.bridge.ReactContext
import com.facebook.react.common.LifecycleState
internal class ReactLifecycleStateManager(
private val bridgelessReactStateTracker: BridgelessReactStateTracker
) {
private var state: LifecycleState = LifecycleState.BEFORE_CREATE
val lifecycleState: LifecycleState
get() = state
@ThreadConfined(UI)
fun resumeReactContextIfHostResumed(currentContext: ReactContext, activity: Activity?) {
if (state == LifecycleState.RESUMED) {
bridgelessReactStateTracker.enterState("ReactContext.onHostResume()")
currentContext.onHostResume(activity)
}
}
@ThreadConfined(UI)
fun moveToOnHostResume(currentContext: ReactContext?, activity: Activity?) {
if (state == LifecycleState.RESUMED) {
return
}
currentContext?.let { context ->
bridgelessReactStateTracker.enterState("ReactContext.onHostResume()")
context.onHostResume(activity)
}
state = LifecycleState.RESUMED
}
@ThreadConfined(UI)
fun moveToOnHostPause(currentContext: ReactContext?, activity: Activity?) {
currentContext?.let {
when (state) {
LifecycleState.BEFORE_CREATE -> {
// TODO: Investigate if we can remove this transition.
bridgelessReactStateTracker.enterState("ReactContext.onHostResume()")
it.onHostResume(activity)
bridgelessReactStateTracker.enterState("ReactContext.onHostPause()")
it.onHostPause()
}
LifecycleState.RESUMED -> {
bridgelessReactStateTracker.enterState("ReactContext.onHostPause()")
it.onHostPause()
}
else -> {
/* Do nothing */
}
}
}
state = LifecycleState.BEFORE_RESUME
}
@ThreadConfined(UI)
fun moveToOnHostDestroy(currentContext: ReactContext?) {
currentContext?.let {
when (state) {
LifecycleState.BEFORE_RESUME -> {
bridgelessReactStateTracker.enterState("ReactContext.onHostDestroy()")
it.onHostDestroy()
}
LifecycleState.RESUMED -> {
bridgelessReactStateTracker.enterState("ReactContext.onHostPause()")
it.onHostPause()
bridgelessReactStateTracker.enterState("ReactContext.onHostDestroy()")
it.onHostDestroy()
}
else -> {
/* Do nothing */
}
}
}
state = LifecycleState.BEFORE_CREATE
}
}