Search code examples
androidbundleactivity-lifecycle

What is the scope of `Bundle savedInstanceState`


In android, we preserve the instance state of our activities by using onSaveInstanceState(Bundle outState).

Now in order to properly implement this it is necessary to have a unique string key for all variables stored inside outState.

I am curious of how unique I need to make this string.

1) Is the Bundle propagated via the Android OS to all active Activities - therefore all keys must be Application & activity namespaced?
(e.g. com.twitter.www.mobile.LOGIN_ACTIVITY.is_logged_in)

2) Is the Bundle only accessed by the Activity in which it was called- therefore all keys must only be Application namespaced?
(e.g. com.twitter.www.mobile.is_logged_in)


Solution

  • The bundle in a particular Fragment or Activity is unique only that Fragment or Activity. They are not propagated throughout the application. It's always good to be verbose/specific in your keys and variables to avoid confusion but you do not have to worry about accessing the wrong instance state bundle or key-value pair from a different activity or fragment.