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
Related
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
In my application, most of the activities has fixed layout - portrait.
So I've mentioned in manifest:
<activity
android:name="com.example.activity5"
android:screenOrientation="portrait" />
But in one or two activities I've to show landscape layout also, in such a way that by default the activity opens in portrait mode. But if user tilts the phone to left/right it changes to landscape. (also if user rotates the phone to upside down, activity should not go to portrait mode).
That is, basically, I want orientation change in 3-way. Default(Potrait) & Left-Right(Landscape).
So, what changes do I need to do in my code & xmls?
Should I choose "sensorPortrait" OR "sensorLandscape"
Should I use android:configChanges="orientation"
I tried few steps, but they are throwing null pointer exception.
I dont know what I'm missing.
P.S. Both orientation have different layouts.
Thank You
I think you want to do this:
1) Just remove android:screenOrientation="portrait", and
2) In your code, in onCreate(), add this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
--(UNRELATED) SIDE NOTE--
To force landscape use this:
<activity android:name="com.example.activity5"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden" />
Write to Main.class(OnCreate):
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
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"
I need a little help in View modes.
.
After launching my Activity, when i press CTL+11 to change view mode from Portrait to Landscape. all activites are called again and they are restarted. A few dialogs are shown again.
Similar behaviour is seen when i change from Landscape to Portrait.
What do i need to do that my main activity does not start again when i change the orientation.
just add following property in androidmenifest.xml under activity tag....
" android:configChanges="orientation" "
Yes, that is standard behavior in android.
If you don't want this behavior, do this in your manifest-file:
<activity android:name="YourActivity" android:configChanges="orientation|keyboard|keyboardHidden"/>
This question already has answers here:
How do I disable orientation change on Android?
(13 answers)
Closed 3 years ago.
I'm writing an android application that uses tabs with different contents (activities).
In one of these activities, I would like to lock the screen orientation to "Landscape"-mode,
but in the other activities, I want the normal orientation (according to sensor).
What I'm doing now is that I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
when I switch to the landscape mode activity, and
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
when I switch back to the other activities. However, this doesn't seem to work,
the whole application locks up. What is the normal approach to this problem?
In the Manifest, you can set the screenOrientation to landscape. It would look something like this in the XML:
<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>
Where MyActivity is the one you want to stay in landscape.
The android:configChanges=... line prevents onResume(), onPause() from being called when the screen is rotated. Without this line, the rotation will stay as you requested but the calls will still be made.
Note: keyboardHidden and orientation are required for < Android 3.2 (API level 13), and all three options are required 3.2 or above, not just orientation.
I had a similar problem.
When I entered
<activity android:name="MyActivity" android:screenOrientation="landscape"></activity>
In the manifest file this caused that activity to display in landscape. However when I returned to previous activities they displayed in lanscape even though they were set to portrait. However by adding
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
immediately after the OnCreate section of the target activity resolved the problem. So I now use both methods.
inside the Android manifest file of your project, find the activity declaration of whose you want to fix the orientation and add the following piece of code ,
android:screenOrientation="landscape"
for landscape orientation and for portrait add the following code,
android:screenOrientation="portrait"