Android onResume() method equivalent in Flutter.io

You can use the WidgetsBindingObserver and check the AppLifeCycleState like this example:

        class YourWidgetState extends State<YourWidget> with WidgetsBindingObserver {

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


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


           @override
          void didChangeAppLifecycleState(AppLifecycleState state) {
            if (state == AppLifecycleState.resumed) {
               //do your stuff
            }
          }
        }

Take in mind that It will called every time you open the app or go the background and return to the app. (if your widget is active)

If you just want a listener when your Widget is loaded for first time, you can listen using addPostFrameCallback, like this example:

    class YourWidgetState extends State<YourWidget> {

          _onLayoutDone(_) {
            //do your stuff
          }

           @override
          void initState() {
            WidgetsBinding.instance.addPostFrameCallback(_onLayoutDone);
            super.initState();
          } 

        }

Info : https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html


If you go to another page, then is called when you comeback

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => SecondPage(),
  ),
).then((value) {
  _refreshFirstPage();
});

You can accomplish this by registering a didChangeAppLifecycleState observer:

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

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

  @override
  void didChangeAppLifecycleState(final AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      setState(() {
        // ...your code goes here...
      });
    }
  }

  @override
  Widget build(final BuildContext context) {
    // ...your code goes here...
  }
}

See WidgetsBindingObserver for more information.

Tags:

Dart

Flutter