10

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?

ryabenko-pro
  • 728
  • 1
  • 7
  • 18

4 Answers4

21

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

iagreen
  • 31,470
  • 8
  • 76
  • 90
  • Save your data in `OnStop()`. It will always be called. – iagreen Dec 19 '12 at 18:11
  • onStop calls before onRetainNonConfigurationInstance, so I may not know is it finishing activity, or just orientation change. – ryabenko-pro Dec 19 '12 at 18:18
  • 2
    That is true. What is the downside of checkpointing your data on configuration change? You can always handle configuration changes yourself by adding `android:configChanges="keyboardHidden|orientation` or similar to your manifest. See [this question/answer](http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation) for the pros and cons. – iagreen Dec 19 '12 at 18:39
  • The other option is if you are using fragments, you can retain the fragment with the action log across configuration changes. – iagreen Dec 19 '12 at 18:41
  • 1
    @iagreen, OnStop() will not always be called. Although it is where we should save data, release memory,... "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." – Nguyen Minh Binh Aug 27 '13 at 08:28
4

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.

Jason
  • 2,503
  • 3
  • 38
  • 46
pooja
  • 107
  • 1
  • 2
  • 7
3

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.

Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
  • onStop calls before onRetainNonConfigurationInstance, so I may not know is it finishing activity, or just orientation change. – ryabenko-pro Dec 19 '12 at 18:16
  • 1
    I don't completely understand why you don't just write to the database while the activity is running...you could do it in a separate thread and use the loader APIs to make sure it's all in sync...seems like a better design pattern to me. with your way, you're kinda always risking people turning off their phone or the OS aggressively reclaiming memory, or who knows what else. – Sam Dozor Dec 19 '12 at 18:52
  • I use action logging, so suddent app termination is not a problem. May be i will use some timer and store data periodacally, while activity running. – ryabenko-pro Dec 19 '12 at 19:10
2

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.

user2314737
  • 27,088
  • 20
  • 102
  • 114