Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didChangeAppLifecycleState doesn't work as expected

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
    });
  }
like image 937
Tony Avatar asked Dec 03 '19 09:12

Tony


1 Answers

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);
like image 107
CopsOnRoad Avatar answered Nov 18 '22 14:11

CopsOnRoad