【问题标题】:How to completely restart a flutter application on didChangeAppLifecycleState (iOS & android devices)如何在 didChangeAppLifecycleState(iOS 和 android 设备)上完全重启颤振应用程序
【发布时间】:2019-07-26 12:34:08
【问题描述】:

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 设备。

我知道我的代码不是一个完整的最小示例 - 如果您需要我设置一个示例供人们尝试,请告诉我。

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    好的,我已经解决了这个问题,但我不能 100% 确定为什么以前使用 exit(0) 的尝试不起作用。

    我已将didChangeAppLifecycleState 函数更改为以下内容。

    @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        super.didChangeAppLifecycleState(state);
        switch (state) {
          case AppLifecycleState.paused:
            print('paused state');
            SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop');
            if (Platform.isIOS) {
              exit(0);
            }
            break;
          case AppLifecycleState.resumed:
            print('resumed state');
            break;
          case AppLifecycleState.inactive:
            print('inactive state');
            break;
          case AppLifecycleState.suspending:
            print('suspending state');
            break;
        }
      }
    

    通过添加以下代码,应用现在可以在 Android 和 iOS 上退出。

    if (Platform.isIOS) {
              exit(0);
            }
    

    如果有人能解释为什么单独使用 exit(0) 不起作用,我很想更好地理解这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-25
      • 2020-04-12
      • 2020-01-13
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 2020-05-11
      • 2020-07-04
      相关资源
      最近更新 更多