在关于AppLifecycle Event的Flutter文档中,它具有4 Event。
在上述4事件中,我可以打印AppLifecycle事件不 Activity ,已暂停,已恢复,但我无法处理暂停事件。
因为我想检查用户是否从任务管理器中杀死/销毁了该应用程序。如果用户杀死/破坏了该应用程序,我想显示安全代码(如PinCode等)。
如何处理暂停事件?
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
AppLifecycleState _appLifecycleState;
FunctionHelper functionHelper = FunctionHelper();
PageController _pageController;
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
_pageController = PageController(initialPage: 0);
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState appLifecycleState) {
setState(() {
_appLifecycleState = appLifecycleState;
});
print(appLifecycleState);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_appLifecycleState == null) {
return Center(
child: Text('This widget has not observed any lifecycle changes.'),
);
} else {
return Center(
child: Text(
'The most recent lifecycle state this widget observed was: $_appLifecycleState'),
);
}
}
最佳答案
Flutter没有任何API可使用Activity flutter/flutter#21982的onDestroy
方法。在暂停事件期间,应用程序会暂时暂停,这等效于Android中的onStop
。当用户杀死或销毁该应用程序时,会在Android中触发onDestroy
,因此无法使用suspending事件对其进行处理。
关于flutter - Flutter:检查AppLifecycle事件挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59366861/