gpt4 book ai didi

flutter - 如何检测 Flutter 应用程序何时从后台返回?

转载 作者:行者123 更新时间:2023-12-04 10:57:56 27 4
gpt4 key购买 nike

我想检测 flutter 应用程序何时从后台返回。

在其他跨应用开发的SDK中,当应用改变这个状态时,通常会有一个监听器。flutter 有类似的东西吗?

最佳答案

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.
}
}

关于flutter - 如何检测 Flutter 应用程序何时从后台返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59056665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com