diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt index 543ab79fba8677..385fd1582a261c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.kt @@ -7,6 +7,8 @@ package com.facebook.react.modules.appstate +import android.app.ActivityManager +import com.facebook.common.logging.FLog import com.facebook.fbreact.specs.NativeAppStateSpec import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.Callback @@ -26,11 +28,27 @@ internal class AppStateModule(reactContext: ReactApplicationContext) : init { reactContext.addLifecycleEventListener(this) reactContext.addWindowFocusChangeListener(this) + val isAppForegroundedByMemoryState = isAppForegroundedByMemoryState() + // pasten: temporary debug log - remove after we validate with real users + FLog.w("AppStateModule", "initial isAppForegroundedByMemoryState = $isAppForegroundedByMemoryState, " + + "reactContext.lifecycleState = ${reactContext.lifecycleState}") + appState = if (reactContext.lifecycleState === LifecycleState.RESUMED) APP_STATE_ACTIVE + // pasten: during cold start appState=APP_STATE_BACKGROUND while tha is actually in the foreground + // best effort foreground detection when LifecycleState.BEFORE_CREATE (which is the initial state) + else if (reactContext.lifecycleState === LifecycleState.BEFORE_CREATE && isAppForegroundedByMemoryState) { + APP_STATE_ACTIVE + } else if (isAppForegroundedByMemoryState) APP_STATE_ACTIVE else APP_STATE_BACKGROUND } + private fun isAppForegroundedByMemoryState(): Boolean { + return ActivityManager.RunningAppProcessInfo().apply { + ActivityManager.getMyMemoryState(this) + }.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND + } + public override fun getTypedExportedConstants(): Map = mapOf(INITIAL_STATE to appState)