class _AppState extends State<App> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
//add an observer to monitor the widget lyfecycle changes
WidgetsBinding.instance!.addObserver(this);
}
@override
void dispose() {
//don't forget to dispose of it when not needed anymore
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
late AppLifecycleState _lastState;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed && _lastState == AppLifecycleState.paused) {
//now you know that your app went to the background and is back to the foreground
}
_lastState = state; //register the last state. When you get "paused" it means the app went to the background.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4415 次 |
| 最近记录: |