Manage App Lifecycle in Flutter!

Image copyright to the respective owner
  • detached:
    The application is still hosted on a flutter engine but is detached from any host views. This means that the app or the engine is initializing while the view is not yet available for the user to interact with.
  • inactive:
    The application is in an inactive state and is not receiving user input. This means that the app is still in the foreground but the user is not interacting with your app. Usually, this state is encountered when the app is open and the user is in a phone call, responding to a TouchID request, opens notification panel, system dialog, picture-in-picture window, etc.
  • paused:
    This state is encountered when the application is not currently visible to the user, not responding to user input, and running in the background.
  • resumed:
    This state is encountered when the application is again in the foreground and is visible and responding to user input.

Step1: Add WidgetsBindingObserver to your class and add the observer.

The first step is the extend our stateful class with WidgetsBindingObserver and add the observer in initState() . We also need to remove the observer from the screen in dispose() method. Here’s a code snippet for the same:

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print('State: $state');
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print('State: $state');
if (state == AppLifecycleState.resumed) print('App Resumed');
}

Don’t forget to connect with me on:

Don’t stop, until you are breathing!💙
- Abhishek Doshi

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Doshi

Abhishek Doshi

1.92K Followers

Google Developer Expert — Dart, Flutter & Firebase 💙💛