

When the Activity is being destroyed, any state related information that is saved in onSaveInstanceState will be available when the Activity comes back online.Īnother situation is when your Activity gets backgrounded.
#Android studio java how to save activity android#
One way that Android handles rotation is to completely destroy and then re-create the current Activity. There is one situation where using the savedInstanceState is almost mandatory: when the Activity gets destroyed during rotation. Rather than attempting to save and recover this information from the Bundle on rotation, the application should have caching and/or database systems set up that are built to deal specifically with these kinds of information. Models (the data kind, not the people kind).Anything downloaded from the web (feed data).In general (with few exceptions), the following kinds of data should never be saved into the Bundle: User-submitted data – If a user writes their username into a text box, they would expect the username to still be present when the Activity is resumed.The current position of the ScrollView should be saved in onSaveInstanceState then restored when the Activity is re-created. Scroll view positions – A user scrolls half way through a ScrollView.During the next onCreate, the selected tab will be available within the Bundle, and the Activity should default to having that tab selected. In onSaveInstanceState the tab selection gets added to the outState Bundle. User selections – A user selects a tab.The savedInstanceState Bundle should only save information directly related to the current Activity state. This method is called when an Activity is being backgrounded (either after onPause() or onStop(), depending on different factors). When this method is called, any state-related data should be placed into the outState Bundle. For example, the savedInstanceState will always be null the first time an Activity is started, but may be non-null if an Activity is destroyed during rotation.Īll activities have an onSaveInstanceState method that can be overridden. If there is no available instance data, the savedInstanceState will be null.

Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle. The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity.
