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 often fails to fire 'background' until the app is foregrounded #50415

Open
Nantris opened this issue Apr 1, 2025 · 11 comments
Open

Comments

@Nantris
Copy link

Nantris commented Apr 1, 2025

Description

AppState fails to fire 'background' until the app is foregrounded

Steps to reproduce

Seems inherent to usage on Android 14, where sometimes it gets called properly before backgrounding is completed but usually it does not.

It seems less likely to occur on the first instance of backgrounding the app.

I'm not sure an Expo Snack can be expected to reliably repro this, but it seems to demonstrate similar behavior. I'm not certain it's not a limitation of Snacks themselves though.

React Native Version

0.76.7

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info

Using Expo, but...


System:
  OS: Linux 6.8 Ubuntu 24.04.2 LTS 24.04.2 LTS (Noble Numbat)
  CPU: (6) x64 AMD Ryzen 7 5800X 8-Core Processor
  Memory: 6.07 GB / 27.36 GB
  Shell:
    version: "5.9"
    path: /usr/bin/zsh
Binaries:
  Node:
    version: 20.18.1
    path: ~/.nvm/versions/node/v20.18.1/bin/node
  Yarn:
    version: 4.5.1
    path: ~/.nvm/versions/node/v20.18.1/bin/yarn
  npm:
    version: 10.8.2
    path: ~/.nvm/versions/node/v20.18.1/bin/npm
  Watchman:
    version: 4.9.0
    path: /usr/bin/watchman
SDKs:
  Android SDK:
    API Levels:
      - "32"
      - "33"
      - "34"
      - "35"
    Build Tools:
      - 33.0.1
      - 34.0.0
      - 35.0.0
      - 35.0.1
    System Images:
      - android-34 | Google Play Intel x86_64 Atom
    Android NDK: Not Found
IDEs:
  Android Studio: Not Found
Languages:
  Java:
    version: 17.0.14
    path: /usr/bin/javac
  Ruby:
    version: 3.2.3
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

No failure. No logs.

Reproducer

Expo Snack Repro

Screenshots and Videos

No response

@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - 0.76.8. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@Nantris
Copy link
Author

Nantris commented Apr 1, 2025

Expo Snacks run in idk what version but the changelog suggests if 0.76.7 has this issue, so will 0.76.8.

@cipolleschi
Copy link
Contributor

@Nantris thanks for the issue, we will look into it!
@cortinico can you look into this?

@cortinico
Copy link
Contributor

@Nantris have you checked for similar issues:
https://github.com/facebook/react-native/issues?q=%22AppState%22%20background%20

If I recall correctly this was already investigated in the past

@Nantris
Copy link
Author

Nantris commented Apr 1, 2025

@cortinico thanks for your reply. I did look and I found at least one that was kind of close but it didn't seem the same. Not sure which it is now and currently on mobile but I can try to find it again if that might have value.

@Lyan37
Copy link

Lyan37 commented Apr 1, 2025

have you try the 'blur' and 'focus' events?

@Nantris
Copy link
Author

Nantris commented Apr 1, 2025

I have tried blur and focus and they've been a help, but I don't think they're an alternative to AppState firing the change event before the app backgrounds.

What I'm seeing is that the background change event fires when the app comes back from background, immediately followed by the active change event.

Edit: And just to reiterate this isn't entirely consistent in our project. Sometimes it does work, but often it does not. I'd say 90% of the time it doesn't work, 10% it does. The 90% of the time it fails, the background event fires after the app is foregrounded again.

@Nantris
Copy link
Author

Nantris commented Apr 4, 2025

@cortinico I wonder if you have any thoughts on this?

The fact background often isn't firing until the app returns to the foreground seems far from the hoped for behavior, but I'm not sure it can be fully replicated in an Expo Snack.

I didn't find any issue that seemed quite like this one.

@cortinico
Copy link
Contributor

@cortinico I wonder if you have any thoughts on this?

Nope I'm sorry I don't have insights on this one (nor capacity to investigate further). If someone from the community could look into it, would be gold

@Nantris
Copy link
Author

Nantris commented Apr 11, 2025

Seems like after my S24 upgraded to Android 15 this works properly. Not 100% sure (didn't hook up the debugger), but it seems like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants