Prasath
Prasath

Reputation: 1121

Will static variable live even after application gets closed?

Consider my application has one activity and I have a static variable in that activity. At first time i initialize the variable , starting a service and exit from the application. At some instance am trying to access the static variable which i initialized before. Sometimes the value is present. but at some times null value is retrieved. plz advise whats going wrong

Thanks in advance.

Upvotes: 4

Views: 2834

Answers (3)

Aleadam
Aleadam

Reputation: 40391

Once the activity is killed by the SO, all the memory resources are back to the system, so you lose the data in that variable. If the activity goes into background instead, the value will be retained

Upvotes: 4

Sver
Sver

Reputation: 3409

If you want to store values even after you exit your app, maybe you should consider using Shared Preferences.

Upvotes: 1

Asaph
Asaph

Reputation: 162801

You cannot rely on a static variable to persist indefinitely. When your application terminates, your statics are gone. If you need to store a value permanently, persist it to a database, filesystem or some other means of storage.

Upvotes: 2

Related Questions