Massab
Massab

Reputation: 1050

Store the state of App before it crashes

I am working on an app that extensively requires server connection and also location updates from GPS. And the app is to be used on road. I have tried to minimize the chances of crashing of app due to internet connection or due to unexpected data from server but still there are chances of crashing. Now what I want to do is to restore the application when it starts again, to previous state therefore I need to store its state before crashing.

Now, what is the better way to do that?

I have tried onDestroy but it isn't called everytime the app crashes.

How can I do That?

Thaks In Advance.

Upvotes: 2

Views: 288

Answers (3)

Nabin
Nabin

Reputation: 11776

It completely depends on how your app works, what kind of information you want to store.

You can save values to shared preference repeatedly(like checkpointing) and when app crashes and the app restarts you can fetch the values from preference

Upvotes: 2

Steve Benett
Steve Benett

Reputation: 12933

Google says:

[..] you should use the onPause() method to write any persistent data (such as user edits) to storage.

If you have to persist data, do it in onPause(), because this lifecycle callback is guaranteed to be invoked, if the process of your Application or the Activity will be killed by the OS.

Upvotes: 2

Vito
Vito

Reputation: 1434

Of course the best way it's make application without crashes, and if app has connection problems just tell that to user. But if you know where crashes happens you can do something like that:

    try
    {
     // some logic with internet connection
    }
    catch (Exception e)
    {
     // here you can save your current progress or some data to database or to shared     preferences
    }

Upvotes: -1

Related Questions