OnSavedInstanceState vs. SharedPreferences

I have 7 active actions with the navigation buttons forward and backward between the others; activists consist of editTexts, Spinners, textViews, TimePickers, DatePickers and checkboxes.

I want the entire user interface to be present and preserved when navigating an instance of the application; however, when the application terminates, everything should be installed by default.

My 8th activity compiles the entire user interface and fits into an email.,. Fyi

I read a lot about both of the onSavedInstanceState and SharedPreferences methods for saving data, as actions go back and forth.,.

Which is better for me?

+6
source share
2 answers

This will depend on how you want to manage the data. Both options (and more) are possible:

  • If you want to fill in once and save the data, even if the application is killed, use SharedPreferences .
  • If this is volatile data that needs to be re-entered at another time (i.e. after a few days), use onSavedInstanceState .
  • If you want to save multiple datasets on the same device, use SQLiteDatabase
+11
source

SharedPreferences

  • Use for things you should always remember, regardless of whether the phone is turned off (for example, for the settings selected on the settings screen of your application

onSavedInstanceState

  • Use this to remember things about the current state of your activity, such as the currently selected tab on the screen. This allows you to recreate the same state after rotation or if the application was killed due to low memory.
  • All things stored in onSaveInstanceState will be forgotten after a reboot, and when a new instance of the action is launched, they will not be transferred, therefore they are only intended to remember the state of activity.

onRetainNonConfigurationInstance

  • Use this to store objects that take a long time to load, so you don’t have to load them again when the phone is rotated.
+7
source

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


All Articles