0

Ddms tells that, when I recall my class called in the past, it performs an onCreate() instead of onResume() that I expected... I noticed that values that I stored in variables of my class in this case are lost and are null. I presume that Android decide to do so to free memory resources (isn't it?). I know that I could use Sharedpreferences to store data in a persistent way and then retrieve... But this is a really dirty way, in my opinion.

So, my question: how to have variables' values preserved also after an onDestroy() (I think?) that Android decided automatically?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Geltrude
  • 1,093
  • 3
  • 17
  • 35

6 Answers6

1

Android will terminate your process at any time when you have no visible activities. For example, the user might go into Settings and terminate your app.

Static data members (my interpretation of your "variables of my class" description) are only meant to be caches, at best. They are no substitute for a persistent data model, whether you use a database, an XML file, a JSON file, or whatever.

So, if you want "variables' values preserved", save them someplace persistent.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

You might find this page on data storage helpful. If your data is primitive, SharedPreferences are the recommended route. (Why do you think they are dirty?) If you need to store an object, you can use internal storage, as documented on that page.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
  • I think that Sharedpreferences and the others are dirty solutions because if you use them you save in a PERSISTENT a variable that you only need to be temporary for the current session... – Geltrude Apr 04 '11 at 06:14
  • I can understand. Unfortunately, Android doesn't have the same notion we're used to of a current session. – Ellen Spertus Apr 04 '11 at 18:48
0

If you don't like SharedPreferences, then you might want to look into Content Providers Even though Content Providers share data across applications, they also provide functionality for you to store persistent data in SQLlite and files that are available only to your app. In this case data stored in this fashion will be available even after closing and restarting your app.

apesa
  • 12,163
  • 6
  • 38
  • 43
0

You can save dynamic state by passing name:value pairs or serializable objects using the Android Architecture and the methods onSaveInstanceState and onRetainNonConfigurationState. You can persist state as explained in the other answers by writing to prefs and doing database writes.

JAL
  • 3,319
  • 2
  • 20
  • 17
0

I've been using custom Application class to store data over application life line.

How to declare global variables in Android?

Community
  • 1
  • 1
harism
  • 6,011
  • 1
  • 36
  • 31
0

http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

Torp
  • 7,924
  • 1
  • 20
  • 18
  • Ok, I think this is the clean solution!! I'm new to Android... Why the other answers (with 86K experience too...) didn't suggest me it? What's wrong? – Geltrude Apr 04 '11 at 06:11
  • It's easy to miss the 'that Android decided automatically' part i guess. – Torp Apr 04 '11 at 09:36