如何使用Flutter(在Android和iOS上)处理应用程序生命周期? [英] How to handle app lifecycle with Flutter (on Android and iOS)?
问题描述
Flutter应用程序中是否有任何Activity
生命周期方法?
Are there any Activity
lifecycle methods in Flutter apps?
赞:
onCreate()
onResume()
onDestroy()
或者:
viewDidload()
viewWillAppear()
使用Flutter开发应用程序时如何处理应用程序生命周期?
How to handle application lifecycle when making an app with Flutter?
推荐答案
当系统将应用程序置于后台或将应用程序返回至前景时,有一种名为
There is a method called when the system put the app in the background or return the app to foreground named didChangeAppLifecycleState
.
小部件示例:
class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
AppLifecycleState _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() { _notification = state; });
}
@override
Widget build(BuildContext context) {
return new Text('Last notification: $_notification');
}
}
还有 ="a href =" https://docs.flutter.io/flutter/dart-ui/AppLifecycleState-class.html"rel =" noreferrer>常量 了解应用程序可以进入的状态,例如:
Also there are CONSTANTS to know the states that an application can be in, eg:
- 无效
- 已暂停
- 恢复
- 暂停
这些常量的用法就是该常量的值,例如:
The usage of these constants would be the value of the constant e.g:
const AppLifecycleState(state)
这篇关于如何使用Flutter(在Android和iOS上)处理应用程序生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!