In my Android application, when I rotate the device (slide out the keyboard) then my Activity
is restarted (onCreate
is called). Now, this is proba
I just simply added
android:configChanges="keyboard|keyboardHidden|orientation"
in the manifest file and did not add any onConfigurationChanged
method in my activity.
So every time the keyboard slides out or in nothing happens.
Put the code below inside your <activity>
tag in Manifest.xml
:
android:configChanges="screenLayout|screenSize|orientation"
onConfigurationChanged is called when the screen rotates. (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
What part of the manifest tells it "don't call onCreate()
"?
Also,
Google's docs say to avoid using android:configChanges
(except as a last resort).... But then the alternate methods they suggest all DO use android:configChanges
.
It has been my experience that the emulator ALWAYS calls onCreate()
upon rotation.
But the 1-2 devices that I run the same code on... do not.
(Not sure why there would be any difference.)
You might also consider using the Android platform's way of persisting data across orientation changes: onRetainNonConfigurationInstance()
and getLastNonConfigurationInstance()
.
This allows you to persist data across configuration changes, such as information you may have gotten from a server fetch or something else that's been computed in onCreate
or since, while also allowing Android to re-layout your Activity
using the xml file for the orientation now in use.
See here or here.
It should be noted that these methods are now deprecated (although still more flexible than handling orientation change yourself as most of the above solutions suggest) with the recommendation that everyone switch to Fragments
and instead use setRetainInstance(true)
on each Fragment
you want to retain.
Add this line to your manifest :-
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
and this snippet to the activity :-
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
The way I have found to do this is use the onRestoreInstanceState
and the onSaveInstanceState
events to save something in the Bundle
(even if you dont need any variables saved, just put something in there so the Bundle
isn't empty). Then, on the onCreate
method, check to see if the Bundle
is empty, and if it is, then do the initialization, if not, then do it.