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) {
super.didChangeAppLifecycleState(state);
print('AppLifecycleState: $state');
if (state == AppLifecycleState.resumed) {
// User has returned to the app (app is in the foreground and active)
print('myState: User returned to the app!');
// Perform actions here, e.g., refresh data, show a message, etc.
} else if (state == AppLifecycleState.paused) {
// App is in the background
print('myState: App went to background');
} else if (state == AppLifecycleState.inactive) {
// App is inactive (e.g., a phone call came in, or app is about to go to background)
print('myState: App is inactive');
}
}