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

When an Activity is destroyed, the AppState listeners are not cleared #45971

Closed
noih opened this issue Aug 11, 2024 · 5 comments
Closed

When an Activity is destroyed, the AppState listeners are not cleared #45971

noih opened this issue Aug 11, 2024 · 5 comments
Labels
API: AppState Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project.

Comments

@noih
Copy link

noih commented Aug 11, 2024

Description

After an Activity is destroyed (such as leaving the app to select file), the useEffect cleanup function does not execute, so subscription.remove is not called. When the App returns to the foreground, AppState.addListener is called again, causing multiple listener registrations that are never cleared.

Since enabling Don't keep activities in Expo web causes the app to restart, I've attached a video and a code snippet.

export default function useAppState(callback: (status: AppStateStatus) => void) {
  const callbackRef = useRef(callback)
  callbackRef.current = callback

  useEffect(
    () => {
      if (typeof callbackRef.current !== 'function') {
        return () => {}
      }

      const onChange = (status: AppStateStatus) => {
        console.log('[useAppState] onChange', status)
        callbackRef.current(status)
      }

      console.log('[useAppState] addEventListener')
      const subscription = AppState.addEventListener('change', onChange)

      return () => {
        console.log('[useAppState] remove')
        subscription.remove()
      }
    },
    []
  )
}

Steps to reproduce

Turning on Don't keep activities in the developer options.

React Native Version

0.74.3

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: macOS 14.6.1
  CPU: (12) arm64 Apple M2 Pro
  Memory: 76.84 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.12.0
    path: ~/Library/Caches/fnm_multishells/8078_1723259080914/bin/node
  Yarn:
    version: 4.2.2
    path: ~/Library/Caches/fnm_multishells/8078_1723259080914/bin/yarn
  npm:
    version: 10.5.0
    path: ~/Library/Caches/fnm_multishells/8078_1723259080914/bin/npm
  Watchman:
    version: 2024.05.06.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/noih/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.4
      - iOS 17.4
      - macOS 14.4
      - tvOS 17.4
      - visionOS 1.1
      - watchOS 10.4
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.3 AI-233.14808.21.2331.11842104
  Xcode:
    version: 15.3/15E204a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.11
    path: /Users/noih/.jenv/shims/javac
  Ruby:
    version: 2.7.6
    path: /Users/noih/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.74.3
    wanted: 0.74.3
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

useAppState addListener
> background
(activity destroyed)

> active
useAppState addListener
> background
> background

(activity destroyed)
> active
> active
...

Reproducer

https://snack.expo.dev/K5dooXY5Llvp4BezvP4Hg

Screenshots and Videos

clip.mov
@react-native-bot
Copy link
Collaborator

⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available - 0.74.5. 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

⚠️ Newer Version of React Native is 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.

@cortinico cortinico added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 Newer Patch Available labels Aug 13, 2024
@deepanshushuklad11
Copy link
Contributor

@noih Can you provide more info i.e is it happening in android old arch too and what about iOS ?

@noih
Copy link
Author

noih commented Aug 15, 2024

OK, give me some time.

@noih
Copy link
Author

noih commented Aug 15, 2024

@noih Can you provide more info i.e is it happening in android old arch too and what about iOS ?

I just built the latest version 0.75.1 and found that this issue no longer exists, whether on the new architecture or the original one, on iOS or Android. So I believe it has been resolved, and I'm closing this issue.

@noih noih closed this as completed Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: AppState Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project.
Projects
None yet
Development

No branches or pull requests

4 participants