Commit e55261b
authored
[android] harden AppStateModule initial state (facebook#57)
## Issue
During app startup, react-native's `AppState.currentState` can incorrectly report `background` when the app is actually in the foreground. This happens because:
1. `AppStateModule` relies on `onHostResume` and `onHostPause` event subscriptions to track state
2. The initial state can incorrectly initialize to `APP_STATE_BACKGROUND` when `reactContext.lifecycleState === LifecycleState.BEFORE_CREATE` (which occurs before root view attachment)
## Solution
This PR adds an `isAppForegroundedByMemoryState()` method that provides a more accurate initial state detection when the React context is in `BEFORE_CREATE` lifecycle state.
## Future Improvements
While this is a quick workaround to address the immediate issue, potential future improvements:
1. Leveraging Android activity lifecycle to set initial state to `RESUMED` on `onCreate`
2. Mapping `LifecycleState.BEFORE_CREATE` to `unknown` instead of incorrectly mapping to `background`1 parent 8f5bcca commit e55261b
File tree
1 file changed
+18
-0
lines changed- packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appstate
1 file changed
+18
-0
lines changedLines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
29 | 36 | | |
30 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
31 | 43 | | |
32 | 44 | | |
33 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
34 | 52 | | |
35 | 53 | | |
36 | 54 | | |
| |||
0 commit comments