0

Possible Duplicate:
Android: Saving a state during Android lifecycle

i want to save some values in a file before the user terminates the activity.In which method should i implement this?

Apart from using a file , or sql lite is there a way of storing complex data such as a layout (that has dynamically changed ) ?

"onDestroy() Note: do not count on this method being called as a place for saving data!"

Community
  • 1
  • 1
zSt
  • 300
  • 1
  • 3
  • 10

4 Answers4

2

Layouts are bound to the context of the Activity that holds it, so you do no want to save full View objects to files because the Activity that it's attached to is void.

The easiest method (depending on the amount and type of data you want to save) is using the SharedPreferences library. It's a file I/O wrapper that makes saving and retrieving data pretty simple. You can save the specific layout data in the onPause() method, the rebuild the layout with the specifications in onResume().

If the data you need is too complex for SharedPreferences, you'll have to save it to use other methods for saving (found in that link). The process for rebuilding the layouts will be the same.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • if i have for example a tablelayout that has been changed and new rows have been added(2 textview each row , 10 rows ),i should retrieve each row's contents , save them , and rebuild the layout in onResume? the file that i mentioned above was used to save 2 integers (for practice) – zSt Oct 06 '11 at 14:56
  • Yes. Save them using one of the methods in `onPause()` (when the Activity leaves view) and rebuild them in `onResume()` (when the Activity is about to come into view). – DeeV Oct 06 '11 at 14:59
1

I would call all the stuff you want to pass to the bundle in your onPause method. Here is some stuff on the android lifecycle and why not try storing stuff in csv if you want to load from file. (comma seperated values) (althoughthat is mainly used for tile based games)

Jack
  • 2,625
  • 5
  • 33
  • 56
0

you can use SharedPreferences , refer the doc , and this tutorial to understand how to do it :)

Houcine
  • 24,001
  • 13
  • 56
  • 83
0

Why not save the data using a SharedPreference? Here are the android docs: http://developer.android.com/guide/topics/data/data-storage.html#pref

slayton
  • 20,123
  • 10
  • 60
  • 89