2

Is there any way to avoid static data loss in Android if device is kept idle ?

I am having static object which will store some values from activity UI. If device is kept idle for 4-5 hours that static object will be removed to use memory for other running applications.

If I am doing it wrong, is there any other way to avoid this ?

Rahul
  • 352
  • 1
  • 4
  • 17

2 Answers2

3

You can't avoid it. Android can kill your process at any time and does not guarantee to call you back.

This only happens if your app goes to the background. Save your data in onPause() and reload it in onResume(). See the Android documentation for Activity lifecycle for more information.

Simon
  • 14,407
  • 8
  • 46
  • 61
0

No, that's how Android is meant to work.

Simply, if data are meant to be persistent, ie if its lifecycle spans beyond the one of the Activity which created it, save it on a persistent storage (ie internal memory), in a flat file or in a database depending if the data has an inner structure or not.

Raffaele
  • 20,627
  • 6
  • 47
  • 86