React-Native App Lifecycle
⚛ Provides functionality to determine whether the app is in the foreground or background and notifies you when the state changes.
It implements the Lifecycle for Android and falls back to the AppState for iOS.
Why Use This?
The original AppState API provided by React Native behaves differently between Android and iOS, particularly regarding the background
state:
- On iOS, the
background
state signifies that the entire app is in the background. - On Android, the
background
state indicates that the React Native Activity is in the background, which might not necessarily mean the entire app is in the background.
By using react-native-applifecycle
, you can handle these differences seamlessly across both platforms.
Install
Install dependency package
yarn add react-native-applifecycle
Or
npm i -S react-native-applifecycle
Usage
The App Lifecycle API is compatible with the original AppState.
Subscribing to the change
listener:
import {AppLifecycle} from 'react-native-applifecycle';
const App = () => {
useEffect(() => {
const listener = AppLifecycle.addEventListener('change', state => {
});
return () => listener.remove();
}, []);
return <View />;
}
export default App;
Getting the current state with hook
:
import {useAppLifecycle} from 'react-native-applifecycle';
const App = () => {
const currentLifecycle = useAppLifecycle();
return <View />;
}
export default App;
For more details, see the Sample Project.
Contribute
New features, bug fixes and improvements are welcome! For questions and suggestions use the issues.
Star History
License
The MIT License (MIT)
Copyright (c) 2024 Douglas Nassif Roma Junior
See the full license file.