문제

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