2

I have a single screen application which .For now, When a user has left the application for less than a minute or so, the app is still on the phones ram and the user can pick from where he left.

I want a feature where, when the user comes back to the app screen after leaving the app, the screen can refresh because contents change..

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
KEVIN
  • 59
  • 2

1 Answers1

3

Assuming that you are using a stateful widget add a widgetbinding observer to it

class _yourClassState extends State<YourClass>
    with WidgetsBindingObserver {
// Here you can override initstate, build etc
}

Now widgetBindingObserver will also let you override didChangeAppLifeCycleState which will allow you to capture the state of the app.

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print("app in resumed from background"); //you can add your codes here
        break;
      case AppLifecycleState.inactive:
        print("app is in inactive state");
        break;
      case AppLifecycleState.paused:
        print("app is in paused state");
        break;
      case AppLifecycleState.detached:
        print("app has been removed");
        break;
    }
}
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Can you add a little context around your code? Where do we put this for example? – nvoigt Jul 11 '22 at 05:00
  • It is an override method which is written like how we write initstate or build. This method can listen to events when the app is resumed or paused – Kaushik Chandru Jul 11 '22 at 05:03
  • It feels like you just copied a block of code and left out the actual explanation of how it answers the question. Where does this code go? Into which class? What does that class need to be derived from? Does the class need to implement something for it to get called? Just a hint, the answer is *yes*, this won't work by just copy/pasting it somewhere. – nvoigt Jul 11 '22 at 05:20
  • Edited my answer to add more info. Thank you for letting me know – Kaushik Chandru Jul 11 '22 at 07:10
  • Looks good now :) – nvoigt Jul 11 '22 at 07:10