什么时候调用AppLifeCycleState.detached? [英] When AppLifeCycleState.detached gets called?

查看:429
本文介绍了什么时候调用AppLifeCycleState.detached?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经:

class _PageState extends State<Page> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print('state = $state');
  }

  @override
  Widget build(BuildContext context) => Scaffold();
}

AppLifeCycleState类具有4个回调,其中3个

AppLifeCycleState class has got 4 callbacks, 3 of them

- active
- paused
- resumed

似乎可以工作,但detached在任何情况下都不会工作.

Seems to work but detached never worked in any case.

我阅读了文档,但无法理解在实际情况下,任何人都可以共享相关代码,何时何地调用它?

I read the documentation but couldn't understand it in practical scenario, can anyone share a relevant code, when and where does it get called?

推荐答案

如文档所述

detached→const AppLifecycleState该应用程序仍托管在 不稳定的引擎,但已脱离任何主机视图.

detached → const AppLifecycleState The application is still hosted on a flutter engine but is detached from any host views.

当应用程序处于此状态时,引擎运行时没有 看法.引擎可以附加视图 最初是初始化的,或者是由于视图被破坏之后 导航器弹出.

When the application is in this state, the engine is running without a view. It can either be in the progress of attaching a view when engine was first initializes, or after the view being destroyed due to a Navigator pop.

仅当您的家庭小部件进入后台(按android设备的后退按钮)时,您才能在HomeScreen上重现以上问题

You can reproduce above issue on HomeScreen only when your home widgets go in the background(Press back button of android device)

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}



class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  didChangeAppLifecycleState(AppLifecycleState state) {
    if (AppLifecycleState.paused == state) {}
    print("Status :" + state.toString());
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Book'),
      ),
      body: Center(
        child: Text('Home Screen'),
      ),
    );
  }
}

您可以在其他屏幕上生成上述内容,并在单独的地方打电话,可以通过在小部件的任何click事件上以编程方式关闭应用程序来完成

You can produce above mention thing on other screens and have a call at detached, you can do by closing application programmatically on any click event of your widget

Android:

SystemChannels.platform.invokeMethod('SystemNavigator.pop');

iOS:

exit(0)

这篇关于什么时候调用AppLifeCycleState.detached?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆