Where is the bundle of onSaveInstanceState saved?

后端 未结 5 1959
旧巷少年郎
旧巷少年郎 2020-12-15 18:21

I would like to know where the bundle \"outState\" of the method onSaveInstanceState(Bundle outState) is stored.

Is it stored in memory or in the device storage?

5条回答
  •  孤街浪徒
    2020-12-15 19:10

    To store data only for application lifetime (ie temporarily), use the onSaveInstanceState(Bundle) activity event

    This data will only be held in memory until the application is closed, the data will be available any time that this activity starts within the current lifetime of the application.

    Explanation: if data is stored here by activity A then the application shows a different activity or rotates the screen (hence closing A) and then returns to A the data can be retrieved to populate the controls. However if the application is closed and opened again the data will be gone and the controls will revert to their default values.

    Example of use: storing text typed in by user and selections making up an order, blog entry, message, etc...

    Note:

    It’s important to notice that only the Activity is destroyed and recreated, not your whole application! An Android application can consist of many Activities, Services and ContentProviders! If the application is closed (for example by pressing the “Back” Button, then all values will be gone. savedInstaceState is only there to preserve data temporary when an Activity is destroyed/recreated, not the application itself.

    If you want to preserve data permanently, you need to save it either as Preferences or in a ContentProvider/database.

提交回复
热议问题