didChangeAppLifecycleState doesn't work as expected

邮差的信 提交于 2020-07-09 06:09:39

问题


I hope I understand how didChangeAppLifecycleState worked correctly.

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());
    }
  }

....

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
    });
  }

回答1:


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.


There are mainly 4 states for it:

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.

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


Edit:

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
  }
});

And from PageB, you'll can use

Navigator.pop(context, true);


来源:https://stackoverflow.com/questions/59153666/didchangeapplifecyclestate-doesnt-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!