question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AppState change doesn't fire on launch on iOS

See original GitHub issue

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()}];

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:22
  • Comments:23 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
slootmaekersdirkcommented, Sep 18, 2018

+1 Ihave the same issue on RN 0.57 (debug mode)

4reactions
asutulacommented, May 22, 2018

I see the same issue as well, and as @markusekblad mentioned, it seems to only appear in release builds. I suspect this is just a timing issue since release builds likely load all the RN javascript much earlier/faster than debug builds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

App state turn to active from background automatically
App state turn to active from background automatically ... Hello, everybody. I'd like to know one skill when developing an iPhone application. What...
Read more >
AppState - React Native
AppState can tell you if the app is in the foreground or background, ... AppState won't change but the blur event will get...
Read more >
Appstate keep on getting change in React native in Android
I had the same problem. Do not use AppState. Is faulty. the problem lies within RN's definition of "background". react-native uses android's ...
Read more >
Using AppState in React Native to improve performance
You can easily update the user status according to the change in the AppState — e.g., online when the user is interacting with...
Read more >
Working with App State and Event Listeners in React Native
When AppState changes happen · Minimising and opening the app, to and from the Home screen. Upon doing so, the app switches between...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found