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.
Related
I have a DialogFragment in my Android app, and I need to set android:configChanges="orientation|screenSize, I want to load different layout for my DialogFragment in portrait or landscape, but now Android will not automatically do this for me, I think I need to update the layout in onConfigurationChanged , but I don't know how to do it, any sample code will be better, thank you!
Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.
try this code and changed your configuration :
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
This attribute informs to Android system that you are going to handle orientation and screenSize changes for this Activity. So instead of destroying and recreating your Activity, Android will just rotate the screen and invoke one of the lifecycle callback method which is onConfigurationChanged(Configuration).
try this
I finnally find my solution, but I think it maybe only work for my situation, my situation is I want to show a imageView in portrait and not show in landscape, so I set the visibility as visiable or gone in function onConfigurationChanged' and it works
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.
Background
Each time the device is being rotated, Android will destroy the current activity and re-create it with the new configuration.
An easy way to avoid activity re-creation is by adding "configChanges" values to the manifest ("screenSize" and "orientation" as I recall).
The problem
If you use the "configChanges" attribute, the app won't respect the orientation lock feature that some (or all?) Android devices have, which prevent anything from being rotated when the screen is being rotated.
What I've tried
I tried playing with the "screenOrientation" attribute values, yet none of them achieved what I wanted.
The question
How can I prevent the activity from being re-created when the screen changes its orientation, while also respecting the user preference of orientation-lock?
Is it perhaps possible to get the value of this preference, so that I could let the activity lock its orientation when it's being set?
OK, never mind. I think I just missed the correct one:
android:screenOrientation="user"
Together with the other configChanges values, this works perfectly.
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).
This is probably a beginner question, but I've already tried looking for an answer and couldn't find one. I was testing an Android app I made on my phone and it was working fine until I switched from portrait to landscape, just to see what would happen. It pretty much made the app go back to the home page, so I'm guessing it calls onCreate() every time the orientation changes. Is it possible to prevent an orientation change from restarting everything, and just have it switch all the views to fit landscape or portrait mode?
This question has been answered thousand times, but ...
You should add in you Manifest.xml for your activity android:configChange="orientation"
and then override onConfigurationChanged(newConfig) method and do not add anything inside, since you are telling you app to do nothing when config changes occur.
Whenever the orientation changes onCreate() method gets called again and again.
Inorder to prevent that add android:configChanges="orientation|keyboardHidden|screenSize"
to the Manifest file, and then override onConfigurationChanged() method.