Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
esprehn opened this issue Apr 13, 2018 · 23 comments
Closed

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

esprehn opened this issue Apr 13, 2018 · 23 comments
Labels
Bug Platform: iOS iOS applications. Priority: Mid Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@esprehn
Copy link
Contributor

esprehn commented Apr 13, 2018

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()}];
@react-native-bot
Copy link
Collaborator

Thanks for posting this! It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest release, v0.55?

Thank you for your contributions.

@esprehn
Copy link
Contributor Author

esprehn commented Apr 13, 2018

Yes it still reproduces.

@radko93 radko93 added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed ⏪Old Version Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. labels Apr 13, 2018
@markusekblad
Copy link

I'm having a similar issue on RN55.2 but only in release builds... which took some time to debug.

@react-native-bot
Copy link
Collaborator

Thanks for posting this! It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest release, v0.55?

Thank you for your contributions.

1 similar comment
@react-native-bot
Copy link
Collaborator

Thanks for posting this! It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest release, v0.55?

Thank you for your contributions.

@react-native-bot
Copy link
Collaborator

It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest release, v0.55?

@esprehn
Copy link
Contributor Author

esprehn commented May 16, 2018

Yes this still reproduces.

@asutula
Copy link

asutula commented 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.

@asutula
Copy link

asutula commented May 22, 2018

Actually let me correct what I said previously. I don't see any initial AppState change firing after launch at all in debug builds in iOS. The first change event happens if I put the app into the inactive/background states by tapping the home button on the sim. Will investigate release build behavior next.

@yung93
Copy link

yung93 commented Jun 7, 2018

It seems only happening on >= iOS11 and in release mode.

@kahoowkh
Copy link

kahoowkh commented Jun 7, 2018

Same with @yung93. Got the issue on iOS 11 release mode only.

@spedy
Copy link

spedy commented Jun 9, 2018

not working for us on debug either

@akiradeveloper
Copy link

reproduced in 0.46 and so annoying.

@joelgetaction
Copy link

I'm seeing cases where the app state never transitions to active and stays at unknown. Any ideas?

@Eyesonly88
Copy link
Contributor

I also see this issue in RN 55.3. I get appState 'unknown' when it should be 'active'.

@jp928
Copy link

jp928 commented Aug 22, 2018

Does anyone test on 0.56?

@jhalborg
Copy link

+1, RN 0.55 / Expo SDK 27. Fires correctly in dev environment, but not on production build

@slootmaekersdirk
Copy link

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

@kodayashi
Copy link

I'm still seeing this as well on 0.57

@elizamaria
Copy link

Any update on the issue? It happens to me on IOS on release mode as well.

@vitriolix
Copy link

I am seeing this as well with React Native 0.59.4, on iOS 11.4.1

@stale
Copy link

stale bot commented Aug 29, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 29, 2019
@stale
Copy link

stale bot commented Sep 5, 2019

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Sep 5, 2019
@facebook facebook locked as resolved and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Platform: iOS iOS applications. Priority: Mid Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests