Question

I want my function to back up information to a file any time the current running activity was disrupted in any way, but i am confused which activity lifecycle to use; should i use paused activity or stopped activity?

btw, in this link there are detailed information about this activities but I just couldn't be sure. http://developer.android.com/guide/components/activities.html#Lifecycle

Thank you.

Was it helpful?

Solution

The best solution is to save data in onPause(). This will ensure that your app never loses the data. onPause() is called when the app has been moved to the background, while onDestroy() is called when the app is being destroyed.

The Android developer guide says the following about onPause():

This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on.

and also this:

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one.

As you can see in the below diagram (which you've probably seen already), the first thing that is called when the app leaves the user's view is onPause(). Sometimes, the user comes back to the app, sometimes the system destroys the app, sometimes the user swipes it away. It's not always clear about the state of the app when onStop() and onDestroy() are called, so onPause() is the best choice as it is always called immediately when the app leaves the user's and your variables are still intact.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top