From 5c0dcddc0fe8faba89bdc15ce3e871e31d626a6e Mon Sep 17 00:00:00 2001 From: Oleg Bogdanov Date: Thu, 24 Jan 2019 11:18:28 -0800 Subject: [PATCH] Expose initialAppState constant from Android native AppState module (#19935) Summary: This PR addresses TODO left in _AppState.js_ The goal is to align AppState module implementation between iOS and Android. iOS native module exposes _initialAppState_ constant that AppState.js relies on, while Android doesn't expose that constant and js implementation uses a fallback. This PR aims to remove a need for a fallback Pull Request resolved: https://github.com/facebook/react-native/pull/19935 Reviewed By: hramos Differential Revision: D13774044 Pulled By: ejanzer fbshipit-source-id: 05d27e702cb9aeedf14293158bfd50004df4726b --- .../react/modules/appstate/AppStateModule.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java index 4eebd65da..ee158be0f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/appstate/AppStateModule.java @@ -14,9 +14,13 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; +import com.facebook.react.common.LifecycleState; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter; +import java.util.HashMap; +import java.util.Map; + @ReactModule(name = AppStateModule.NAME) public class AppStateModule extends ReactContextBaseJavaModule implements LifecycleEventListener { @@ -26,10 +30,15 @@ public class AppStateModule extends ReactContextBaseJavaModule public static final String APP_STATE_ACTIVE = "active"; public static final String APP_STATE_BACKGROUND = "background"; - private String mAppState = "uninitialized"; + private static final String INITIAL_STATE = "initialAppState"; + + private String mAppState; public AppStateModule(ReactApplicationContext reactContext) { super(reactContext); + reactContext.addLifecycleEventListener(this); + mAppState = (reactContext.getLifecycleState() == LifecycleState.RESUMED ? + APP_STATE_ACTIVE : APP_STATE_BACKGROUND); } @Override @@ -38,8 +47,10 @@ public class AppStateModule extends ReactContextBaseJavaModule } @Override - public void initialize() { - getReactApplicationContext().addLifecycleEventListener(this); + public Map getConstants() { + HashMap constants = new HashMap<>(); + constants.put(INITIAL_STATE, mAppState); + return constants; } @ReactMethod