onSavedInstanceState vs. SharedPreferences

前端 未结 2 1422
半阙折子戏
半阙折子戏 2020-12-18 05:05

I have 7 activites all with back and forth navigation buttons between the rest; activites consist of editTexts, Spinners, textViews, TimePickers, DatePickers, and checkboxes

2条回答
  •  执念已碎
    2020-12-18 05:59

    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.

提交回复
热议问题