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?

有帮助吗?

解决方案

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

其他提示

You can use sharedPreferences.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top