[英]flutter didChangeAppLifecycleState never runs
I implemented the WidgetsBindingObserver
, but the app is NEVER sent to the background so it doesn't recognize the AppLifecycleState.resumed
我实现了
WidgetsBindingObserver
,但该应用程序从未发送到后台,因此它无法识别AppLifecycleState.resumed
this is the current implementation这是当前的实现
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
print('\n\ndidChangeAppLifecycleState');
switch (state) {
case AppLifecycleState.resumed:
print('\n\nresumed');
_mymethod();
break;
case AppLifecycleState.inactive:
print('\n\ninactive');
break;
case AppLifecycleState.paused:
print('\n\npaused');
break;
case AppLifecycleState.detached:
print('\n\ndetached');
break;
}
}
to simulate the process i do the next in android模拟我在android中做下一个的过程
WidgetsBindingObserver
WidgetsBindingObserver
打开小部件when returning to the app i can see my widget on screen, the app doesn't restart, but NONE of the prints appears on the console not event the print('\\n\\ndidChangeAppLifecycleState');
返回应用程序时,我可以在屏幕上看到我的小部件,该应用程序不会重新启动,但控制台上没有任何打印显示,而不是事件
print('\\n\\ndidChangeAppLifecycleState');
and _mymethod();
和
_mymethod();
is never executed永远不会被执行
The WidgetsBindingObserver mixin requires a bit more work than merely implementing the interface. WidgetsBindingObserver mixin 比仅仅实现接口需要更多的工作。 You also need to add the following to your widget state class:
您还需要将以下内容添加到您的小部件状态类:
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.