How do i prevent an orientation change of a background activty? - android

I'm preventing an activty from changing to landscape mode via
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
everything works fine except if i call a new activity. When the new activity turns to landscape mode the background activity changes too. How do i prevent this?

Fix your activity to portrait mode when in onPause.
onPause(){
// Your code to make this app fixed to portrait when in background
}

Related

Oreo (8.1.0): Portrait orientation issue

I'd like to have all activities in portrait orientation.
So I have tried both
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
in onCreate(), onPause() or onResume() but the screen will flip to landscape and back to portrait in case of I rotate device in landscape from the beginning before opening the app.
But if I remove setRequestedOrientation method and just add android:screenOrientation="portrait" in AndroidManifest.xml only then it works (but let's imagine if I have 30 activities so it's better to have in BaseActivity instead)
How to reproduce
BaseActivity has method in onCreate(), onResume(), onPause() setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Rotate device in landscape orientation.
Open app, Activity A is flipping from landscape to portrait.
Start Activity B from Activity A, Activity B is flipping from landscape to portrait.
Note
I have checked already
Android 8.1 screen orientation issue: flipping to landscape a portrait screen
Activity rotating itself and back to normal in android 8.1
but it didn't work (it works only if orientation is defined in AndroidManifest)
If someone knows how to fix please share the solution
Thank you so much.

How to lock screen orientation when anctivity isn't recreated

I have an activity used to display a video (VideoView).
By default the user can change screen orientation to see the video.
I avoid destroying/recreating the activity upon orientation changes because of latency issue.
The rotation is painless without any sound break.
android:configChanges="keyboardHidden|orientation|screenSize"
What I want to do now is add a button that will force the activity to remain on current orientation.
The problem is that because the activity isn't recreated after the rotation, I don't know which method to override to avoid the rotation to happen.
Overriding onConfigurationChanged is useless because the rotation already happened.
Use setRequestedOrientation().
E.g. to lock the orientation to portrait, call:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In your activity.

Android: screen goes back to portrait mode when turning screen off

In my manifest file I specified android:configChanges="orientation". And I noticed that when turning off the screen while in landscape the onConfigurationChanged() method is called once with portrait status and then called again with landscape status when waking up the screen.
What is the reason for this ? Is there any way to disable it ?
Use this in your manifest file--
<android:configChanges="orientation|keyboardHidden" >
This will never let your screen change when you rotate the screen landscape or portrait.
Use this one in your android menifest
android:configChanges="orientation"
for landscap
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if you want unspecified
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
The problem seems related to the lock screen which forces the layout back to portrait when switching screen off/on.
I solved my issue by ignoring calls to onConfigurationChanged() between the calls to onPause() and onResume()

android:configChanges="keyboardHidden|orientation" Issue Android

Am using android:configChanges="keyboardHidden|orientation"for switching between portrait and landscape modes. But when user opens softkeyboard in portrait and changes to landscape keyboard should hide or dissappera but its not happening with that.
If you are intercepting orientation without overriding public void onConfigurationChanged(Configuration newConfig) in your Activity it is normal that the keyboard remains open.
You can either
override this method to reset the display
or avoid intercepting orientation to let the Activity restart (in which case your Activity will be reinitialized and onCreate() will be called again)
I'm not sure in it but I think that you need to write keyboardHidden|orientation|portrait (or landscape).

onConfigurationChanged not getting called the first time

I've a problem trying to capture the onConfigurationChanged event. This is the scenario:
Activity A starts (listens to onConfigurationChanged)
Phone rotated to landscape mode (onConfigurationChanged being called). Start activity B.
Activity B starts (listens to onConfigurationChanged) (LANDSCAPE)
Activity B rotates back to portrait (onBackPressed event raised). Activity B is destroyed and A is called back.
Activity A resumes
Phone rotated to landscape mode. The onConfigurationChanged is not called this time.
Phone rotated to portrait mode. The onConfigurationChanged called.
Phone rotated to landscape mode. The onConfigurationChanged called.
Why step 6 don't call onConfigurationChanged event? it doesn't make sense at all. Do you know what could be the issue?
I have the same problem, I update UI onResume() to fix it.
I had exactly the same issue. I still don't understand this behavior of android system, but you can use onOrientationChanged of OrientationEventListener instead of configuration change handling.
See this answer (example is not perfect but shows the way): https://stackoverflow.com/a/13844242/554281
I have faced same problem and was stuck in this for more than a week. Then I have prepared a sample APP with tabHost and reproduced the same issue on the sample app. After playing around with that, I found that this is a bug of android's tabHost. So, I have migrated tabHost to FragmentTabHost and found the problem is gone.
The simulation of the Issue with tabHost:
Let, there are two tab i.e A and B
Simulation 1 (Arise the bug)
Arrive on A in portrait mode
Go to B on portrait mode
Rotate B to landscape
Back to A on landscape mode.
Rotate A to portrait (onConfigChanged method of tabActivity gets fired. But onConfigChanged of A activity doesn't get fired)
Simulation 2 (Works fine)
Arrive on A in portrait Mode
Go to B on portrait mode (continue rotating as you want, but stop on portrait mode)
Back to A on portrait mode.
Rotate A to landscape (both onConfigurationChanged method of tabActivity and A activity gets fired)
Summary: If you get back to the screen in the same orientation you got out, both onConfigurationChanged() will get called.

Categories

Resources