react / react/react-native

AppState 'change' fires redundant 'active' when app is already active the first time the listener is added on Android iOS Prod

未关闭
#45,418 3 条评论 10 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

API: AppState Issue: Author Provided Repro Platform: Android
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

Description

On Android and iOS PROD when the AppState listener 'change' is added the very first time it automatically also fires the listener with nextState as "active" even though the app is already "active".

This issue occurs specifically on Prod and not Dev. On Dev it works as expected where the listener is not fired the first time it's added. listener is only fired the first time when the app goes to the background.

EXPECTED BEHAVIOR:
The behavior should be the same with dev and prod where the listener does not automatically fire with 'active' when it's added the very first time.

Steps to reproduce
  1. Create a new RN app with npx react-native init androidAppStateIssue
  2. Add the AppState 'change' listener on a button click as follows
  const addAppStateListener = () => {
    // read 'AppState.currentState' to register current state on prod
    // comment, uncomment this console.log to see difference on prod
    console.log('INITIAL', AppState.currentState);

    AppState.addEventListener('change', nextAppState => {
      console.log('nextAppState', nextAppState);
    });
  };

return (
...
<Button title="addAppStateListener" onPress={addAppStateListener} />
)
  1. Build on Android/iOS, generate apk/app and install on emulator/simulator (PROD).
  2. Click the addAppStateListener button and notice that the listener is NOT automatically fired with "active" when the listener is added.
  3. Comment the line console.log('INITIAL', AppState.currentState); and rebuild, re-install app.
  4. Click the addAppStateListener button and notice that the listener IS automatically fired with "active" when the listener is added
React Native Version

0.74.3 (current latest)

Affected Platforms

Other (please specify)
Android and iOS PROD

Output of npx react-native info
System:
  OS: macOS 14.5
  CPU: (10) arm64 Apple M1 Pro
  Memory: 89.75 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.20.4
    path: ~/.nvm/versions/node/v18.20.4/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v18.20.4/bin/yarn
  npm:
    version: 10.7.0
    path: ~/.nvm/versions/node/v18.20.4/bin/npm
  Watchman:
    version: 2023.12.04.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.12.1
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK:
    API Levels:
      - "33"
      - "34"
    Build Tools:
      - 30.0.3
      - 33.0.0
      - 33.0.1
      - 34.0.0
    System Images:
      - android-33 | Google APIs ARM 64 v8a
      - android-34 | Google APIs ARM 64 v8a
      - android-35 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10671973
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.9
    path: /Users/ashwsrir/.jenv/shims/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.3
    wanted: 0.74.3
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
Stacktrace or Logs

When listener is added for the first time in dev vs prod. (make sure to comment the console.log('INITIAL', AppState.currentState)on app)

dev

Running "androidAppStateIssue
listener is added

prod

Running "androidAppStateIssue
listener is added
nextAppState', 'active' <== additional event fired
Reproducer

https://github.com/ashwinkumar6/androidAppStateIssue

Screenshots and Videos

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 React Native 0.74.3 实现中的 AppState.addEventListener 和 AppState.currentState 行为开始,然后使用链接的 androidAppStateIssue 示例,在 development 和 production build 中重现该问题。跟踪 Android 和 iOS 上的第一次 listener 注册,并验证当应用已经处于 active 状态时,添加它不会发出不必要的 active 事件。

由索引模型根据 Issue 内容生成。

评估

技术栈
android, ios, javascript, react-native
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。