Android lock screen orientation and leaving onCreate() untouched - android

I am running something in onCreate() upon initialisation.
If a user rotates the screen, it recalls onCreate().
I want to disable screen rotation and let onCreate() run ONLY upon the initial initialisation.
Is it enough to add android:screenOrientation="portrait" to the manifest or will onCreate() still be run?
Thanks!

If you put android:screenOrientation="portrait" in your Manifest the Phone doesnt handle orientation changes and onCreate() doesn't get called again.
So: YES it is enough!
You can easily check it if you set a Debug-Marker in your onCreate() and then rotate your phone!

If you hold your Activity in Portrait or in Landscape, the rotation will no longer happens. So the onCreate() will run to the end though you will try to rotate your device

Add in your manifest:
android:configChanges="orientation"
android:screenOrientation="portrait"

Related

Screen still can rotate even if orientation is set to Portrait

I know the tile seems to be very confusing, but yeah, it happened to my application. Ok, here's the story.
Here's the key settings in Manifest:
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|keyboardHidden"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
In my Activity.java, I also override onConfigurationChanged(Configuration config), which will print out current orientation.
Here comes the most interesting part. I start a rotatable Application, say Gallery (assume it can rotate), from my application in landscape using Intent. Keep the phone in landscape, and press back key. The newly launched activity, in this case Gallery, will quit, and my application will restart. In theory, my application should display in portrait since I have set the screenOrientation to portrait. However, in reality, my application just stays in portrait for 1 second, and then jumps to landscape, and jumps back to portrait again.
I start a thread to print out requestedOrientation of my application after onRestart is called. And I find out that the output is always "1", which is "portrait". Yet, the output from onConfigurationChanged gives my a current orientation of landscape!
So, I'm totally confused. How can this happen? Can someone give me some advice?
May be you use screen orientation in an activity. But another activity will be rotate. in below example ActivityA's orientation will not change but ActivityB will be rotate. If you write android:screenOrientation="portrait" in app tag and not mention anything in activity. It doesn't work anything. So please mention screenOrientation in activity.
<activity
android:name=".ActivityA"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".ActivityB">
</activity>
are you using android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|keyboardHidden"
inside your application or under activity

Change Screen Orientation but not to lock

I want to change my device screen orientation programmaticaly. For this I can use:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
But this would lock my screen for entire activity lifecycle. I just want to change the screen orientation so that I can receive onConfigurationChangedcallbacks.
This is how my manifest looks like for an Activity:
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
So, my question here is if there's a way to change screen orientation for once and clear the flags from setRequestedOrientation
Try to use onResume() .
I think this might help you.
Want to create a view on Run time. In onResume() create the view.

onConfigurationChanged never fires

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

Can a live wallpaper lock the screen in portrait mode?

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.

Ensure screen orientation is fixed to portrait and NEVER changes

I would like my Android app to instantiate it's home screen activity one time only. I am managing the back stack appropriately to achieve this but have just discovered an orientation issue when the app starts up.
Visually this orientation change only shows itself on the emulator. (probably runs too fast to be observed on a device).
Here's what happens :: -->
activity.onCreate()
activity.onDestroy()
activity.onCreate()
This sequence makes sense and is caused by the change in orientation. What does not make sense (to me) is that it happens at all because I have done the following to prevent an orientation change :: -->
AndroidManifest.xml contains
android:screenOrientation="portrait"
for all my activities and in the home screen activity onCreate() method, I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
So why do I get an orientation change?
Add android:screenOrientation="portrait" in your manifest file where you declare your activity like this
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait"/>
if you want to do using java code
try
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
before you setContentView for your activity in onCreate()
see here

Categories

Resources