Question

I am working on an Android app and in the main activity I have a number I would like to save when I exit out of the app. It works when I pause/resume the activity but when I go to the task manager and stop the app and then restart it, the number goes back to 0. I tried using the onSaveInstanceState and onRestoreInstanceState methods but it didn't work. Any suggestions?

Was it helpful?

Solution

There's a distinction between the Activity state and application state.

The Instance state is associated with the Activity, and can be used to store information temporarily to re-populate an Activity during a single lifetime of the application. I.e., it's useful when your Activity is being destroyed by the OS. But when your application is killed (like you're doing from the task manager), the instance state is wiped.

If you want to store information persistently across multiple lifetimes of the application, you should use the SharedPreferences framework.

See here for more information: http://eigo.co.uk/labs/managing-state-in-an-android-activity/#How-to-Store-State-Information

OTHER TIPS

You can use sharedPreferences.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top