Siavash
Siavash

Reputation: 7853

Do static variables get preserve if activity/app gets destroyed in Android

I have a singleton that contains some data that I want to hold globally for the app.

I know that if the user has navigated away from my app, and if the system needs resources it will kill my app (or just the activity, not sure). Then if the user opens my app again, it will restore the activity and wil have save the local activity variables automatically.

Will this process also restore the static variables that were set in my global singleton class out side of the activity?

Upvotes: 1

Views: 1539

Answers (2)

Rishab Surana
Rishab Surana

Reputation: 2167

If you want to save the state of that variable you can either save that in sharedpreference in your onPause method of an activity and then retrieving that variable again in your onResume method

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

No. Only what is in your saved instance state Bundle might be restored if Android terminates your process, and then only if the user returns to your app relatively quickly (say, within 30 minutes).

The saved instance state Bundle is mostly for "in flight" data (e.g., partially-filled-out forms) that you would not mind losing if the user does not return to you for a while. For anything else, save the data yourself, whether to local storage or "the cloud".

Upvotes: 2

Related Questions