[Flutter] AppLifecycleState
The observable lifecycle events are:
inactive
— The application is in an inactive state and is not receiving user input. This event only works on iOS, as there is no equivalent event to map to on Androidpaused
— The application is not currently visible to the user, not responding to user input, and running in the background. This is equivalent toonPause()
in Androidresumed
— The application is visible and responding to user input. This is equivalent toonPostResume()
in Androidsuspending
— The application is suspended momentarily. This is equivalent toonStop
in Android; it is not triggered on iOS as there is no equivalent event to map to on iOS
flutter: AppLifecycleState.inactive
flutter: AppLifecycleState.paused
flutter: AppLifecycleState.inactive
flutter: AppLifecycleState.resumed
如果我們想監聽App級別的生命週期需要使用WidgetsBinding來監聽WidgetsBindingObserver.
Example:
class extends StatefulWidget {
LifeCycle({Key key}): super(key: key);
LifeCycleState createState() => new LifeCycleState();
}
class LifeCycleState extends State<LifeCycle> with WidgetsBindingObserver {
@override
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
Widget build(BuildContext context){
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text(''),
),
body: new Center(
child: new Text(''),
),
),
);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
print('AppLifecycleState.inactive');
break;
case AppLifecycleState.paused:
print('AppLifecycleState.paused');
break;
case AppLifecycleState.resumed:
print('AppLifecycleState.resumed');
break;
case AppLifecycleState.suspending:
print('AppLifecycleState.suspending');
break;
}
@override
void dispose(){
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
Last updated