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.
Related
I have stumbled on a problem, that, when the screen turns off while being in LANDSCAPE orientation, certain devices "rotate" the app back to PORTRAIT position (because the lockscreen is PORTRAIT only or something like that). I did a little research before posting this, and most popular work-around is to modify app's configuration change process to prevent activity being recreated after the configuration has changed.
But disabling activity recreation is not a solution for me, because my app supports both orientations with sepparate layout's etc.
So i would like to find out, is it possible to disable the configuration change only in special cases (Screen turned off and orientation is landscape)? Or is the right way to override onConfigurationChanged() then manually manage activity recreation inside that function (i guess simply setting different layout resources when orientation is changed simply wont cut it)?
Or is the right way to override onConfigurationChanged() then manually manage activity recreation inside that function?
Yes to an extent.
You cannot set the configChanges attribute programmatically. I guess it's to do with the way an Activity is created. They're created from the XML first and then the overridden methods in your activity implementation are invoked. There's nothing in the API that lets you change the configChanges attribute.
Now in your case it doesn't sound like you need to. If you support both orientations, then if the user locks the device and it rotates back why does it matter? From a UX perspective we know it's in portrait mode again. So should your app when it opens back up.
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.
I want to make my application displayed only in portrait orientation, so I have put android:screenOrientation="portrait" in the Activity tag in AndroidManifest.xml, and have put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the Activity's onCreate method.
This works to lock the orientation to portrait, however, when the Activity starts, it shows itself once, then shows itself again, so you see a sort of flash. I can confirm that onCreate is being called twice as well.
This flash is causing further force closes later in my application.
How can I eliminate this flash?
Edit
I had a splash screen displaying before the activity I had described. It was being run twice, and therefore ran this activity twice (via an Intent) twice. I fixed this issue by checking if this activity had already been stared using an intent in the splash screen class, and it had, not to run it again. The fix was more of a workaround than a fix, but I hope it helps people.
I believe that using the Activity's configChanges attribute (in the manifest) should solve your problem. ConfigChanges attribute documentation
However, you are just going around another problem. What you really should address is why is that provoking FC? There is absolutely no reason for that to happen so you also should solve that problem. More info in handling runtime changes.
Please note: using the first approach is acceptable to address the performance/UX issues though.
Just in case you wonder why onCreate is called twice, once I got into this problem and I think that it was related to having the orientation fixed and then having another activity being created but in a different orientation. Before the second activity was started, the former activity changed into the orientation of the latter. And remember that keyguard is also an Activity! I'm not sure if this happens for this reason though.
don't put : "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);".
Just let the AndroidManifest do his job. I thing what you did is :
Tell your App to go only in Portrait mode
Tell your Activity to be in Portrait mode, wherever it was already like this or not (The flash have to come from this).
I could be wrong.
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.
I have application which works only in landscape orientation . I set that in xml layout. When I'm starting application it works ok . But when the application is started and next if I lock the phone and then unlock, the application first 1-2 seconds is in portrait mode and then in landscape. Is it possible to skip theese 2 seconds?
Try defining it explicitly in your manifest. Add android:screenorientation="portrait" to each <activity> element.
Not to be a downer, I really hope you solve this. Just wanted to throw out there that I have noticed this sort of behavior(typically for me the apps are supposed to stay in portrait but they show landscape for about 2 seconds) on multiple applications on my Sprint HTC Hero, including the default home application and many of the default included apps (contacts, etc). I am beginning to suspect it's an android problem. Again, feel free to disagree with me, but I just wanted to say that I have noticed this on a lot of apps & you're not alone here :/ However, there are some apps that I have not noticed it on, so either a) I am just getting lucky or b) there is a correct solution
Execute this in your onCreate(), just before setcontentview:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
I use this method in my apps and I never notice the orientation changing. It's locked solid into the requested orientation.
To be provocative, actually, what he needs to put in each activity element in his manifest is:
android:screenOrientation="landscape"
and not "portrait"
:P
I hope this help; put these on every activity-element in your Android Manifest-file.
android:configChanges="orientation"
android:screenOrientation="landscape"
Of course it is landscape; my brain was not the quickest this time.