Reputation: 9706
I'm interested to know which methods are overridden, when an Android device is rotated (i.e. when configuration changes)?
onSaveInstanceState(...)
, onConfigurationChanged(...)
, onRestoreInstanceState(...)
- something similar to this?
It will be also interesting for me to listen about the whole process connected with changing a configuration. Thanks.
Upvotes: 3
Views: 28139
Reputation: 119
Go to Manifest and add: android:configChanges="orientation|screenSize"
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:configChanges="orientation|screenSize"
/>
In Main Activity: Copy and Paste this code:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
All Done -
Upvotes: 2
Reputation: 67286
When you rotate the device your Activity will re-create and all the variables will be re-initialized. So, in that case if you want to some values to remain same on Rotation also you can store their state using the onSaveInstanceState()
and you can restore in onCreate()
again by checking Bundle is not null.
if(savedInstanceState != null){
// get the restore value from the Bundle
}
Where as onConfigurationChanged()
will be called when you rotate the Device(Note that this will only be called if you have selected configurations you would like to handle with the configChanges
attribute in your manifest). From the parameter you will get the new device configuration.
If you don't want your Activity to get re-created on rotation of Device then you have to add this line to your activity tag in the AndroidManifest
file.
android:ConfigChanges="keyboardHidden|orientation"
Upvotes: 5
Reputation: 1883
According to the android developer reference you have to use:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
For more information see: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
You could be interested in seeing: http://developer.android.com/guide/topics/manifest/activity-element.html#config
Here you can find all configuration listener you can listen.
Remember to put them in the android manifest:
<activity
android:configChanges=["orientation"]
. . .
</activity>
Hope this helps...
Upvotes: 6
Reputation: 109237
From android developers...
To retain an Object during a runtime configuration change:
onRetainNonConfigurationInstance()
method to return the Object you would like to retain.getLastNonConfigurationInstance()
to recover your Object.Android calls onRetainNonConfigurationInstance() between onStop() and onDestroy() when it shuts down your Activity due to a configuration change. In your implementation of onRetainNonConfigurationInstance(), you can return any Object that you need in order to efficiently restore your state after the configuration change.
A scenario in which this can be valuable is if your application loads a lot of data from the web. If the user changes the orientation of the device and the Activity restarts, your application must re-fetch the data, which could be slow. What you can do instead is implement onRetainNonConfigurationInstance()
to return an object carrying your data and then retrieve the data when your Activity starts again with getLastNonConfigurationInstance()
.
Look at here for more info Retaining an Object During a Configuration Change you can also find example over their..
Thanks..
Upvotes: 3