Shadab Hussain
Shadab Hussain

Reputation: 23

Reload screen when react-native app is re-opened

In a react-native application, when we click on Home button the application is minimized. If after that I click on the application then application is resumed where it was minimized last. I want to refresh the last open screen every time that the application is maximized. What should I do to achieve this?

I tried {useIsFocused} from @react-navigation/native and I also tried

const unsubscribe = navigation.addListener('focus', () => {
     alert('focus')
});

But it's not working.

Thanks in advance

Upvotes: 0

Views: 5018

Answers (1)

Samin
Samin

Reputation: 273

Appstate is what you're looking for. Change componentDidMount to useEffect and state to useState if required. More info here

import {
    AppState
} from 'react-native';

constructor(props) {
   super(props)
   this.state = {
       appState: AppState.currentState
   }
}

componentDidMount() {
  AppState.addEventListener('change', this._handleAppStateChange);
}

_handleAppStateChange = (nextAppState) => {
    if (
            this.state.appState.match(/inactive|background/) &&
            nextAppState === 'active'
        ) {
            //Todo: Reload task here
        }
        this.setState({ appState: nextAppState });
};

Upvotes: 4

Related Questions