Skip to content

AppState change doesn't fire on launch on iOS #18836

@esprehn

Description

@esprehn

AppState's change event doesn't fire on first launch on iOS, but does fire on Android. The currentState does change however because the RCTAppState.getCurrentAppState call eventually returns with the active state.

Environment

Environment:
OS: macOS High Sierra 10.13.3
Node: 8.9.4
Yarn: 1.5.1
npm: 5.6.0
Watchman: 4.7.0
Xcode: Xcode 9.2 Build version 9C40b
Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed)
react: 16.2.0 => 16.2.0
react-native: 0.54.3 => 0.54.3

Steps to Reproduce

AppState.addEventListener('change', () => console.log('changed', AppState.currentState));
console.log('start state', AppState.currentState);
setInterval(() => {
    console.log('current state', AppState.current);
});

This prints:

start state unknown
current state unknown
current state active
current state active
current state active
...

Expected Behavior

The change handler should fire since the currentState does change from unknown to active.

Actual Behavior

change handler never fires.

To fix either:

  1. The RCTAppState.getCurrentAppState callback needs to compare currentState to appStateData.app_state and manually fire the event
  2. RCTAppState.getCurrentAppState should be removed and native side should always fire one event at startup to initialize the state, then you don't need this separate init step either.

I'm not sure how to do (2) since it means dispatching an event always on startup, not sure where you'd put that code? (1) is easy enough change RCTAppState.getCurrentAppState to not take a callback at all and just dispatch an event instead.

RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback
                  error:(__unused RCTResponseSenderBlock)error)
{
  callback(@[@{@"app_state": RCTCurrentAppBackgroundState()}]);
}

becomes

    [self sendEventWithName:@"appStateDidChange"
                       body:@{@"app_state": RCTCurrentAppBackgroundState()}];

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPlatform: iOSiOS applications.Priority: MidStaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions