onSavedInstanceState vs. SharedPreferences

对着背影说爱祢 提交于 2019-11-29 07:10:23

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

  • If you want to fill once and keep the data even if the app gets killed, use SharedPreferences.
  • If it's volatile data that will have to be reentered differently some other time (i.e., days later), then use onSavedInstanceState.
  • If you want to keep multiple datasets on the same device, then use a SQLiteDatabase

SharedPreferences

  • Use for things that should always be remembered, no matter if the phone is turned off (eg for settings chosen in the settings screen of your app

onSavedInstanceState

  • Use this for remembering 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 a rotation or if the app was killed due to low memory.
  • The things saved in onSaveInstanceState will be forgotten after reboot, and when starting a new instance of an activity they will not be passed, so they are only for remembering the state of the activity

onRetainNonConfigurationInstance

  • Use this for storing objects which take a long time to load so that you don't have to load them again when the phone is rotated.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!