didChangeAppLifecycleState无法按预期工作 [英] didChangeAppLifecycleState doesn't work as expected

查看:1667
本文介绍了didChangeAppLifecycleState无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能理解 didChangeAppLifecycleState 的工作方式。

I hope I understand how didChangeAppLifecycleState worked correctly.

我有A页和B页。当我单击页面B的后退设备按钮( Navigator.of(context).pop(); )时,我期望 didChangeAppLifecycleState 会被调用,但不会。

I have page A and page B . When I click the back device button from page B ( Navigator.of(context).pop(); ), I expect didChangeAppLifecycleState in pageA will get called, but it doesn't.

PageA

class _ABCState extends State<ABCrList> 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) {
    if (state == AppLifecycleState.resumed) {
      setState(() {
        print(...);
      });
    }else{
      print(state.toString());
    }
  }

....

这是pageA中的 initState

This is the initState in pageA. The function used to call backend service.

@override
  void initState() {
    super.initState();  
       _bloc.getList(context);  // return list and populate to ListView
    });
  }


推荐答案

您的思维方式这是Android在 onResume 上工作的方式,但是在Flutter中,事情并非以这种方式发生。

The way you're thinking it is Android's way where onResume works, but in Flutter, things don't happen this way.

通常,当系统将应用程序置于后台或将应用程序返回至前景时会调用此方法。

Generally, this gets called when the system puts the app in the background or returns the app to the foreground.

主要有4个州:

恢复 :该应用程序可见并且正在响应用户输入。

resumed: The application is visible and responding to user input.

非活动状态:该应用程序处于非活动状态并且处于活动状态

inactive: The application is in an inactive state and is not receiving user input.

已暂停:该应用程序当前对用户不可见,未响应用户输入,并在后台运行。

paused: The application is not currently visible to the user, not responding user input, and running in the background.

已分离:该应用程序仍托管在Flutter引擎上,但与任何引擎都分离主机视图。

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

编辑:

当您从 PageA 导航到 PageB 时,请使用类似以下内容:

When you're navigating to PageB from PageA, use something like:

Navigator.pushNamed(context, "/pageB").then((flag) {
  if (flag) {
    // you're back from PageB, perform your function here
    setState(() {}); // you may need to call this if you want to update UI
  }
});

在PageB中,您可以使用

And from PageB, you'll can use

Navigator.pop(context, true);

这篇关于didChangeAppLifecycleState无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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