2

I have an activity which fetches huge set of data from the network and i want to handle the scenario for this activity if the DVM decides to destroy this activity (due to low memory condition) i don't have to fetch the same data again from the network. How do i go about doing this.please suggest.

Regards, Kumar

Deva
  • 3,919
  • 1
  • 27
  • 38

1 Answers1

2

Depending on the data you need to savior you could pass in the data (if it's primitive types) into the Bundle of the Activity and override the onPause method of the activity. The onPause method will be called whenever the Activity closes.

See the activity lifecycle for reference http://developer.android.com/reference/android/app/Activity.html

There are other ways to save the data - you could write it directly to a file on the SD card and then close the stream in the onPause method if the Activity is closed by the OS without finishing the data.

Then when the Activity is opened again you call the onResume method of the Activity and read the data saved inside the Bundle or the file written to the SD card.

Darwind
  • 7,284
  • 3
  • 49
  • 48
  • Can't the same thing be done using the onSavedInstanceState(). – Deva Oct 04 '11 at 19:37
  • Well basically it can, but according to the documentation on Activity the onSaveInstanceState() is not a part of the lifecycle, so it won't necessary be called every time the Activity is started or stopped. Reference: http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle). So if the OS plans to kill off the Activity it will kill the Activity and not necessarily call the onSaveInstanceState(), but it will call the onPause() every time it kills an Activity. – Darwind Oct 05 '11 at 15:27
  • Their could be 2 scenario's : 1> When the activity being killed is in the foreground 2> When the activity being killed is in background. Will the situations vary? From android developer for 2 http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle which says "If its process needs to be killed, when the user navigates back to the activity , its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.". So when to use onPause – Deva Oct 05 '11 at 18:27
  • I mean to say when to use onPause() to save the data and when to use onSavedInstanceState(). – Deva Oct 05 '11 at 18:57
  • A good post: http://stackoverflow.com/questions/5506852/android-what-to-put-in-onpause-and-onsaveinstancestate The official documentation states that you should use onSaveInstanceState() only for saving state of say the UI and the onPause() for saving persistent data. The keypoint is that the onPause() will always be called as it's part of the lifecycle. You can not be sure the onSaveInstanceState() will be called all the time, because it is not part of the lifecycle. – Darwind Oct 05 '11 at 19:01
  • Thanks Darwind for the clarification this helped. – Deva Oct 05 '11 at 19:04
  • Yeah sorry for being a bit blurry at first - it's a hard topic in my opinion - both to understand and to explain ;) – Darwind Oct 05 '11 at 19:19