Home > front end >  React-Native Background State
React-Native Background State

Time:01-25

My app seems to randomly get stuck in a loading state after a user goes to lock screen or another app and then returns on iOS.

Scenario:

User is using the app to view images then clicks lock screen button.

[iPhone lock screen1

The user unlocks their phone and the app is now stuck infinitely loading.

Generic app in loading state

The only way to end the loading state is by shutting the app down and restarting it. It then works as normal.

This doesn't always happen. But the issue only crops up after the app has idled.

Does anyone have any idea what I could do to 'wake up' the app? I'm finding it hard to debug as it doesn't happen on the emulator.

CodePudding user response:

Use this code in your root container (main screen)

import { AppState } from "react-native";

useEffect(() => {
    const subscription = AppState.addEventListener("change", appState => {
      if (appState === "active") {
        console.log("App has come to the foreground!");
        // call the function to remove loader here
      }
      console.log("AppState", appState);
    });

    return () => {
      subscription.remove();
    };
  }, []);

Ref : AppState React Native

  •  Tags:  
  • Related