我有一个应用程序,当
最初的回答:我期望该应用程序会退出,再次打开后会重新启动,但实际上它根本不会退出。它只是在暂停状态下继续运行之前的位置。这种行为仅在iOS设备上发生。
我明白我的代码不是一个完全工作的最小示例-如果您需要我设置一个供人们尝试的示例,请告诉我。
AppLifeCycleState
为 .paused
时,我希望完全重新启动它。这个功能在 android (Pixel 3 XL Api 28)
上似乎可以正常工作,但在 iOS 设备上不行。
iOS 原生等效方法:
在我的本机 iOS 版本的应用程序中,在 AppDelegate 的 applicationDidEnterBackground 函数中运行 exit(0)。
我尝试使用 WidgetBindingObserver
使 MyApp 成为有状态的,并监听状态的变化。在 .paused 状态下,我尝试执行 exit(0)
和 SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
位于 MyApp 状态内部
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.paused:
print('paused state');
SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
//I have also tried exit(0); here.
break;
case AppLifecycleState.resumed:
print('resumed state');
break;
case AppLifecycleState.inactive:
print('inactive state');
break;
case AppLifecycleState.suspending:
print('suspending state');
break;
}
}
最初的回答:我期望该应用程序会退出,再次打开后会重新启动,但实际上它根本不会退出。它只是在暂停状态下继续运行之前的位置。这种行为仅在iOS设备上发生。
我明白我的代码不是一个完全工作的最小示例-如果您需要我设置一个供人们尝试的示例,请告诉我。