Android - saving application state

I want to save the state of the application in order to restore it after another launch. Is it better to use the onSaveInstanceState method and save it in the Bundle or use SharedPreferences?

thanks

+6
source share
3 answers

It depends on your intention. Using onSaveInstanceState() is only a smart solution if you want to maintain state during configuration changes and other restart events. If you are aiming to truly maintain the state of the application outside the application life cycle, you should consider using either SharedPreferences, or perhaps even use a database.

+8
source

I may have the same designs as some other posters here (I have been seriously developing applications since July 2012), but I found a solution that combines SharedPreferences as well as onSaveInstanceState ().

My application has a splash screen activity that reads values ​​from SharedPreferences and assigns them to the appropriate variables. In addition, each action that I do has its own onSaveInstanceState () method, and I take care of all the data that I need to store in SharedPreferences there, in every action. Since onSaveInstanceState () is started before the application or activity closes normally, it must support data values ​​under all normal circumstances.

This may not be the most effective solution for the code, especially in large applications with many types of activities, but, as far as I know, my tests protect your application from data loss in 99% of cases.

If a more experienced developer would like to listen and confirm or disprove it, I am sure that this will enrich the question and answer.

+1
source

I'm sure onSaveInstanceState () is the best option.

Here's a more detailed explanation: Saving Android activity state using instance persistence state

0
source

Source: https://habr.com/ru/post/899194/


All Articles