I am trying to stop Activity recreation due to device orientation change by setting android:configChanges="orientation|screenSize" to my Activity and listening for orientation change by overriding onConfigurationChanged method. I am testing it on Nexus 5, but it is not working in Android L device because onConfigurationChanged never fires. It just sticks to portrait mode, and does not rotate.
Is it a bug in Android L, am I doing something incorrectly, or they have introduced some other way to do this?
Are you usjng android:screenOrientation in your manifest? If so, remove it. This might be silly but, do you have auto rotation enabled?
Aashir is correct! With this, add android:configChanges="orientation|keyboardHidden|keyboard|screenSize" to your activity in menifest.xml
Related
I have a problem. I have an App, which has a fix orientation (portrait) set in the manifest.xml.
android:screenOrientation="portrait"
But I do want to change the orientation of an Dialog, that I create programmatically in one of my fragments. Problem is, that the super method onConfigurationChanged(Configuration newConfig)is never been called, because of my manifest declaration.
Is it possible to detect that the phone has been turned and change the orientation of the dialog (Without destroying / recreating everything like normally)?
EDIT:
"Activity and Fragments should stay in portrait mode, Dialog should detect changes and orient"
Hope this question is understandable ;)
Thanks!
I think that a viable solution would be to remove the android:screenOrientation tag and then override the onConfigurationChanged() method to do what you need.
I got an app that is locked in landscape orientation. Now, when I change the devices settings to set its orientation locked to portrait, some problems occur.
After a bit of testing, it seems that for each Activity that is started, the onCreate is called twice.
First time for portrait orientation, second time as an orientationChange with orientation set to landscape. The first orientation is wrong, and probably has something to do with the device being locked to portrait.
In the app, the orientation is forced to landscape programmatically (using setRequestedOrientation), and I prefer to keep it that way, if possible, for various reasons.
Any ideas about how to avoid this behavior?
Thanks in advance!
Ok, I found a solution myself.
After a lot of searching and digging in the code, the solution was fairly simple.
All I had to do was add:
android:configChanges="orientation|screenSize"
... to all my Activities in the manifest file.
This tells the app that I will handle configuration changes myself in the code. Instead of doing this, I didn't add any handlers for these config changes. This way, my Activities aren't recreated when the orientation change happens at the creation of those Activities. I can do it this way, because my app is locked in a certain orientation.
This post came closest to my problem, but it's solution didn't work for me:
Android: set activity orientation BEFORE onCreate, but not in manifest (HDMI plugged in issue)
This post gave me the answer:
http://www.acnenomor.com/1053172p1/oncreate-were-called-twice-after-implementing-setrequestedorientation
Try setting up the orientation directly in the manifest instead of programmatically. Under your activity tag in AndroidManifest.xml add android:screenOrientation="landscape".
This should force the activity to be directly created in the correct orientation.
How to test the landscape mode on Android Emulator (Mac) ?
When I press ctrl+fn+11 the emulator turns into landscape mode, but the activity screen remains in portrait orientation. What should I do?
Does your activity have android:configChanges="orientation" in its manifest entry? That would explain the behavior, since you are then responsible for overriding onConfigurationChanged() and redrawing your views yourself.
If you don't need to handle it explicitly, just remove the configChanges attribute and your activity will be destroyed and recreated (you might need to save and restore custom state in this case).
Can a live wallpaper lock the screen in portrait mode? And if so, how?
I should mention that I have seen two supposed answers to this question on SO, one appeared inordinately complicated, I didn't understand it all and the answer was not accepted by the original poster. The second answer didn't work for me.
A third answer involving the use of:
android:screenOrientatin = "portrait" or "landscape"
has been suggested, but it is not clear exactly where this should go in the manifest.
EDIT: have tried putting android:screenOrientation="portrait" in many different places in the manifest, and none of them worked.
EDIT: another answer was to rotate your bitmaps and handle a rotation by just drawing everything sideways - but this looks very ugly because, as you rotate you phone, the OS instigates a rotation animation - which means that you get a horrid jumping effect as you turn the phone.
I'm beginning to suspect that the true answer is simply "no".
Did you try setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ?
In your AndroidManifest.xml you should have something like:
<application
android:icon="#drawable/app_icon"
android:label="#string/nuboLogin"
android:name=".LoginApplication"
android:debuggable="true">
<activity
android:name=".WallPaperActivity"
android:label="#string/wallPaper"
android:windowSoftInputMode="adjustPan"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This should make sure that your Activity runs in portrait mode. If you prefer landscape, you can easily guess what you should modify
Android application restarts the activity when the orientation changes. You can either use
android:configChanges in your manifest. The activity is shut down and restarted by default, when a configuration change occurs at runtime, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
use android:screenOrientatin = "portrait" or "landscape" it will force the app to run in the mode you specify. However it will not prevent the activity from being shut down and restarted.
I have an activity that shouldn't be recreated after an orientation change. I added the following to the activity declaration in the manifest:
android:configChanges="orientation"
On Android 2.3 this works perfectly. On HONEYCOMB_MR2 onCreate is called anyway when change the orientation.
Is there something else that needs to be done on HONEYCOMB_MR2 to prevent recreating the activity after an orientation change?
Apparently using orientation|screenSize (?) prevents onCreate on Honeycomb and (so far) does not seem to break anything in previous Android versions.
android:configChanges="orientation|screenSize"
No idea why this is necessary and I don't really understand the documentation about this new value.
I suspect that in HONEYCOMB_MR2 upon an orientation change the activity considers this as two config changes: orientation and screen size. And both attempt to recreate the activity by default.