Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save data in activity's onDestroy method

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?

like image 496
ryabenko-pro Avatar asked Dec 19 '12 16:12

ryabenko-pro


People also ask

How do you save data on onDestroy?

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.

What is onDestroy () meant for?

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.

Why we should write the data saving code in the onPause () method?

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.


4 Answers

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

like image 80
iagreen Avatar answered Oct 05 '22 00:10

iagreen


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.

like image 42
pooja Avatar answered Oct 05 '22 00:10

pooja


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.

like image 43
Sam Dozor Avatar answered Oct 05 '22 00:10

Sam Dozor


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 eitheronPause()or onSaveInstanceState(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.

like image 38
user2314737 Avatar answered Oct 04 '22 23:10

user2314737