To add a Check in Flutter when an App executes Resume from a background or terminated state, you can use the WidgetsBinding
class’s addObserver
method to listen for the LifecycleState
changes. Here are some steps you can follow:
- Create a class that extends
WidgetsBindingObserver
:
This is where you’ll put your resume logic. You can add checks and perform actions when the app resumes. - Add observer to
WidgetsBinding
:
In your widget’s build method, add the observer.
Here is an example:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show SystemChrome.setPreferredOrientations;
import 'package:flutter/widgets.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
// Resume logic here
print('App resumed');
break;
case AppLifecycleState.paused:
// Pause logic here
print('App paused');
break;
case AppLifecycleState.detached:
// Detach logic here
print('App detached');
break;
case AppLifecycleState.inactive:
// Inactive logic here
print('App inactive');
break;
// not present anymore in the api case AppLifecycleState.unknown
case AppLifecycleState.hidden:
// Unknown logic here
print('App hidden');
break;
}
}
@override
void didChangeMetrics() {
super.didChangeMetrics();
// Metrics change logic here
}
@override
void didChangeLocales() {
super.didChangeLocales();
// Locales change logic here
}
@override
void didChangePlatformBrightness() {
super.didChangePlatformBrightness();
// Brightness change logic here
}
@override
void didUpdateWidget(MyHomePage oldWidget) {
super.didUpdateWidget(oldWidget);
// Update widget logic here
}
@override
void didUpdateWidget(MyHomePage oldWidget) {
super.didUpdateWidget(oldWidget);
// Update widget logic here
}
@override
void dispose() {
super.dispose();
}
}
In the above code, didChangeAppLifecycleState
is where you’ll put your resume logic. This method gets called when the app’s lifecycle state changes.
Note: Don’t forget to remove the observer in dispose
method when your widget is removed from the tree.
Example use case:
You can add checks and perform actions when the app resumes, such as:
- Checking if there are any new notifications
- Fetching data from the server to update your app’s state
- Showing a welcome message or tutorial when the user resumes the app after a long time
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
// Fetch data from server to update app's state
fetchAndUpdateData();
break;
}
}
void fetchAndUpdateData() async {
// Fetch data from server
final data = await fetchDataFromServer();
// Update app's state with the fetched data
setState(() {
// Update your widget's state here
});
}
Very obvious example where resume check may be needed is when your app requires some permissions. https://programtom.com/dev/2025/01/22/example-of-how-to-use-the-permission_handler-package-in-flutter/. Both iOS and Android users may pause your app, remove the permissions and return, resulting in a situation – where the app cannot complete the functionality it is programmed to. So, is is always good idea to program defensively.