I'm writing a tasklist and have Project object, which holds all the tasks (and metadata). I use action log, so when tasks changes i do not save it immediately to database, just keep it in memory to dump in database on activity finish. Activity's onDestroy method is best place for this: if no onRetainNonConfigurationInstance method was called I start service to save project (one's instance is stored in Application). Saving is expensive: In DB project have revision, so I save new data, change current revision and delete previous revision's data. So i do not afraid of suddent application stop.
BUT, aсcording to documentation i must do not count on this method being called as a place for saving data.
Is there any alternative place for saving my data?
According to the Activity Lifecycle documentation you should save your data in onPause() or onSaveInstanceState(Bundle) . The methods onDestroy() and onStop() might never be called before the activity is shut down.
onDestroy: The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.
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. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.
OnDestroy is not always going to be called. From the lifecycle docs --
When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.
Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database
see Stop Your Activity
You should not use onDestroy()
method for saving data. Instead, you should use internal/external storage space or write your code in the onPause()
method.
You should be using onStop
Activity docs
You could also use onPause
, but that will be called whenever you navigate away from the Activity
, including turning off the screen.
According to the Activity Lifecycle documentation you should save your data in onPause()
or onSaveInstanceState(Bundle)
.
The methods onDestroy()
and onStop()
might never be called before the activity is shut down.
Excerpts from the Activity Lifecycle documentation:
protected void onDestroy ()
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either
onPause()
oronSaveInstanceState(Bundle)
, not here.protected void onStop ()
Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With