AppState 'change' fires redundant 'active' when app is already active the first time the listener is added on Android iOS Prod
Personne n'a encore pris cette issue.
- Langage dominant
- C++
- Étoiles
- 127k
- Forks
- 25.3k
- Merge moyen
- 1 j 23 h
- PR mergées (30 j)
- 4
Description
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
- Create a new RN app with
npx react-native init androidAppStateIssue - 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} />
)
- Build on Android/iOS, generate apk/app and install on emulator/simulator (PROD).
- Click the
addAppStateListenerbutton and notice that the listener is NOT automatically fired with "active" when the listener is added. - Comment the line
console.log('INITIAL', AppState.currentState);and rebuild, re-install app. - Click the
addAppStateListenerbutton 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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par le comportement de AppState.addEventListener et AppState.currentState dans l’implémentation de React Native 0.74.3, puis reproduisez le problème à l’aide de l’exemple androidAppStateIssue lié, dans des builds de développement et de production. Suivez le premier enregistrement du listener sur Android et iOS et vérifiez que son ajout n’émet pas d’événement active inutile lorsque l’application est déjà active.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- android, ios, javascript, react-native
- Domaine
- mobile
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100