android – Where is the bundle of onSaveInstanceState saved?-ThrowExceptions

Exception or error:

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?

I am concerned about the security of the data which is stored in the bundle.

How to solve:

I don’t think there’s any way that any malicious background process can get at the bundle data of your application. It is not documented how Android treats the Bundle data. It may or may not be written to disk in the event that your app is cleaned, while backgrounded. However, given that we don’t know whether or not this data is saved to disk, and if it is, given that we have no clue where, and almost certainly don’t have read access to that part of the disk, I wouldn’t worry about some third party process being able to recover that data.

Consequently I’m not clear what you might think the exposure is. Though I may be missing something.
However, in answer to your question, it is absolutely in memory while your app is alive, and if your app is backgrounded it may or may not be written somewhere hidden, but we dont’ know because Google hasn’t told us.

It’s destroyed along with the application when the memory is collected.

###

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.

###

Here is a detailed answer for where the outState Bundle data is saved:

…Bundles are an IPC mechanism, so it’s not going to the filesystem. But now there’s a P involved – which process is it? And what is that process doing with this data? And do I need to be worried about it? It turns out that these instance state bundles are stored in the Activity Manager service. This service is implemented under the package com.android.server.am in the Android source code. Recall that Activities are stacked one on top of another and that Android calls these stacks “Tasks”… Each of these tasks is represented internally with an object of class TaskRecord. This class contains an array of ActivityRecord objects, each of which manages the state of an Activity. ActivityRecord contains a member of type Bundle named icicle. This icicle-bundle is the saved instance state and it is actually stored in the memory space of the Activity Manager service.

Source: https://www.linkedin.com/pulse/android-onsaveinstancestate-bundle-secret-safe-daniel-pietsch/

###

My guess would be in memory, but the best way to protect your data would be not to trust the system and encrypt it. Never trust the client (in this case the client being the OS).

EDIT:

To be clear, I’m not saying encrypt the bundle. Rather I’m saying that any sensitive data should not be put into the bundle. If you must put custom data in the bundle, then encrypt it.

But ultimately you should keep as little sensitive data on the client as possible. This is the same reason a e-commerce site would only show the last 4 digits of a credit card.

###

The documentation as been updated and indicates precisely that the state is serialized to disk:

Saved instance state bundles persist both configuration changes and process death, but are limited by amount of storage and speed because onSavedInstanceState() serializes data to disk.

You can also found a table comparing the differents approches to preserving UI state

Option for preserving UI state

Source: https://developer.android.com/topic/libraries/architecture/saving-states

Leave a Reply

Your email address will not be published. Required fields are marked *