skip to Main Content

I am trying to save some data like what was the visited page, when the application was getting closed(time) in the database just before closing the application, so I can resume my journey after opening the app next time. What will be the best way to implement this?

I tried AppLifeCycleState.detached(Flutter) , but it’s not getting called if user close the app by swiping up from the overview. Also tried to do in native android way(Using onDestroy() method of activity), also not working correctly.

How can I achieve something like storing the last visited page and the time before killing the application.

Thanks for any help.[For native codes please give example in both kotlin and swift]

void didChangeAppLifecycleState(AppLifecycleState state) {
     if (state == AppLifecycleState.detached) {
         // Save the time and page...
     }
}

In native(android) code –

onDestroy() {
    // Save time and page...
}

2

Answers


  1. First,
    Add WidgetsBindingObserver to your state class like;

    class _AppState extends State<App> with WidgetsBindingObserver
    

    *make sure to dispose it in your dispose method like: WidgetsBinding.instance.removeObserver(this)

    Second,
    Use this comparison instead

    if(state==AppLifecycleState.paused)
    {
    //save time and page
    }
    
    Login or Signup to reply.
  2. The app will always go into the paused state before going to termination, always, unless there is a bug in your app that crashes it. So using the paused state is perfectly fine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search