How to save state during orientation change in Android if the state is made of my classes?
EDIT: On newer versions of Android and with the compatibility library, retained fragments are usually the best way to handle keeping expensive-to-recreate data alive across activity destruction/creation. And as Dianne pointed out, retaining nonconfiguration data was for optimizing things like thumbnail generation that are nice to save for performance reasons but not critical to your activity functioning if they need to be redone - it's not a substitute for properly saving and restoring activity state.
But back when I first answered this in 2010:
If you want to retain your own (non view-state) data, you can actually pass an arbitrary object specifically for orientation changes using onRetainNonConfigurationInstance()
. See this Android Developers blog post. Just be careful not to put any Views or other references to the pre-rotation Context/Activity in the object you pass, or you'll prevent those objects from being garbage collected and may eventually run out of memory (this is called a context leak).
Android save state on orientation change
Have you tried using: its work through
<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>
in Manifest file?
It does not work by default because , when you change the orientation onCreate
will be called again and it redraws your view.
If you write this parameter no need to handle in Activity , the framework will take care of rest of things.
It will retain the state of the screen or layout if orientation is changed.
NOTE If you are using a different layout for landscape mode , by adding these parameters the layout for landscape mode will not be called.
Other way and Another way
Save state of activity when orientation changes android
I use in KOTLIN static var / val :
class MyFragment : Fragment()
{
//all my code
//access to static vars -> MyStaticClass.hello
}
class MyStaticClass
{
companion object {
var hello: String = "Static text"
var number_static: Int = 0
}
}
Android : Save application state on screen orientation change
Well if you have the same layout
for both screens then there is no need to do so just add below line in your manifest
in Activity
node
android:configChanges="keyboardHidden|orientation"
for Android 3.2 (API level 13) and newer:
android:configChanges="keyboardHidden|orientation|screenSize"
because the "screen size" also changes when the device switches between portrait and landscape orientation. Documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html
Save data and change orientation
If you have small data, you can save and restore it using onSavedInstanceState
and onRestoreInstanceState
.. for details go through this link Saving data
But in case, you have large data then I must say, you should not allow for the orientation changes(which force your activity to recreate). You can restrict it by adding below line in manifest file :
android:configChanges="orientation|keyboardHidden" // fixes orientation
Save the Tab state during orientation change
That's not the best way. You should use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() to retain the state between config changes. Those methods are specifically for saving state during config changes.
public Object onRetainNonConfigurationInstance() {
return mTabHost.getCurrentTab();
}
public void onCreate() {
...
Integer lastTab = (Integer) getLastNonConfigurationInstance();
if(lastTab != null) {
mTabHost.setCurrentTab(lastTab);
}
...
}
What's the right way to save instance state only on rotation?
Turns out my question was wrong from the beginning.
When the device rotates, Android calls onSaveInstanceState(Bundle outState)
and onRestoreInstanceState(Bundle savedInstanceState)
as a way for the developer to save/restore state. When the activity is destroyed via back button or finish()
, onSaveInstanceState
is not called and the savedInstanceState
bundle that's passed to onCreate()
is null. So trying to answer the "was this created after a rotation or not?" question is irrelevant.
Posted in case someone has the same question and thanks to all that took time to read and help.
Related Topics
Bitmap Recycle with Largeheap Enabled
How Does Push Notification Technology Work on Android
Using Android Vector Drawables on Pre Lollipop Crash
Android - File Provider - Permission Denial
How to Send Http Post Request and Receive Response
How Do Android Screen Coordinates Work
Vectordrawable - Is It Available Somehow for Pre-Lollipop Versions of Android
How to Change the Textcolor on an Android Searchview
Defaulthttpclient to Androidhttpclient
Lost My Keystore for Uploaded App on Android Market
How to Implement a View Holder
Timepicker in Preferencescreen
Get Uri of .Mp3 File Stored in Res/Raw Folder in Android
How to Make an Application Ignore Screen Orientation Change
Android: How to Pass Parcelable Object to Intent and Use Getparcelable Method of Bundle