Android 8.1 screen orientation issue: flipping to landscape a portrait screen

AndroidAndroid 8.1-Oreo

Android Problem Overview


I have all activities in portrait mode except the one that I use to play a video that is always landscape. I found that on Android 8.1 every time I open the video activity and close it the previous activity go to landscape even it's set to "portrait" on the manifest.

  1. Sometimes goes to portrait then to landscape and stay on landscape.
  2. Sometimes goes to portrait then to landscape and finally portrait again.

This is only happening when a go back from a activity that it's landscape.

There is anyone who is experiencing this?

Thanks.

EDIT

I report the bug on Google: https://issuetracker.google.com/issues/69168442

EDIT 2

It seems fixed on Android 9

Android Solutions


Solution 1 - Android

Just came across this problem in my own app.

The solution that works for me is as follows:

onCreate(){
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

onPause(){ 
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  }
}

onResume(){
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  }
}

The above code should go in the activity that is in landscape mode (i.e. the second activity, and the one you press the back button from)

I would like to point out that this solution was not my own, and I have taken it from the #20 post at the following link (which is also noted in the OP):

https://issuetracker.google.com/issues/69168442

I just thought it might be easier for people to access if they don't have to search another page for it.

Solution 2 - Android

This fixed the issue.

Override the onBackPressed() method of Landscape activity and set orientation to Portrait.

@Override
public void onBackPressed() {
    super.onBackPressed();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Solution 3 - Android

If u have DialogTheme like theme=Theme.AppCompat.Light.Dialog in your manifesto, remove the set orientation tag for that Dialog activity from manifesto , It will take the Orientation from the previous activity and put setorientation tag for remaing activites

and for below Versions place this on oncreate

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }

and set theme for application Theme.AppCompat.Light.DarkActionBar no need to add theme to activities

Solution 4 - Android

Using intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK) in previous Activity resolved my issue.

Solution 5 - Android

If you need to support orientation changes on the parent activtiy consider using the current orientation in onPause() of your landscape activity.

onCreate(){
   super.onCreate();
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

onPause(){ 
  super.onPause();
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(getResources().getConfiguration().orientation);
  }
}

onResume(){
  super.onResume();
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  }
}

This answer is based on TheTestSpecimens one.

Solution 6 - Android

from Narmi's answer:

> When you will back to Activity A from Activity B and if you know the > screen's orientation of the Activity A, so set the screen orientation > into the ondestroy of Activity B.

you have to detect if activity is destroying from configuration change, so add field isConfigurationChanged = false, then on onSaveInstanceState method turn it to true and on onDestroy method add this:

> @Override > protected void onDestroy() { > if(!isConfigurationChanged) > setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); > super.onDestroy(); > }

Solution 7 - Android

on the onCreate method of each activity insert this line

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

for the landscape and

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

for the portrait ones

Solution 8 - Android

I had such problem and been trying all of above use cases. Most of them work, but there is one case you should know:

The final reason was the using of fragments inside an activity content layout in case of Android 8. For example: The activity launches in Landscape mode, but the fragment shows you the Portrait layout.

Try to avoid fragments.

Solution 9 - Android

To fix this issue:

When you will back to Activity A from Activity B and if you know the screen's orientation of the Activity A, so set the screen orientation into the ondestroy of Activity B.

@Override
protected void onDestroy() {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onDestroy();
}

Solution 10 - Android

The next workaround may help you to solve the issue.

You must extend all your activities using the one in the code. It takes care of setting and restoring the correct orientations whenever the onPause / onResume methods are called.

The workaround will work for any type of orientation defined in the manifest activity tags.

For my own purposes I extend this class from ComponentActivity, so you may want to change this to extend from Activity, ActivityCompat, or whatever type of activity you are using in your code.

public abstract class AbsBaseActivity extends ComponentActivity
{
	private int currentActivityOrientation   = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
	private int parentActivityOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;

	@CallSuper
	@Override
	protected void onCreate(@Nullable final Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		this.cacheOrientations();
	}

	private void cacheOrientations()
	{
		if (this.currentActivityOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
		{
			final Intent parentIntent = this.getParentActivityIntent();

			if (parentIntent != null)
			{
				final ComponentName parentComponentName = parentIntent.getComponent();

				if (parentComponentName != null)
				{
					this.currentActivityOrientation = this.getConfiguredOrientation(this.getComponentName());
					this.parentActivityOrientation = this.getConfiguredOrientation(parentComponentName);
				}
			}
		}
	}

	private int getConfiguredOrientation(@NonNull final ComponentName source)
	{
		try
		{
			final PackageManager packageManager = this.getPackageManager();
			final ActivityInfo   activityInfo   = packageManager.getActivityInfo(source, 0);
			return activityInfo.screenOrientation;
		}
		catch (PackageManager.NameNotFoundException e)
		{
			e.printStackTrace();
		}

		return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
	}

	@CallSuper
	@Override
	protected void onPause()
	{
		if (this.parentActivityOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
		{
			this.setRequestedOrientation(this.parentActivityOrientation);
		}

		super.onPause();
	}

	@CallSuper
	@Override
	protected void onResume()
	{
		super.onResume();

		if (this.currentActivityOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
		{
			this.setRequestedOrientation(this.currentActivityOrientation);
		}
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSolView Question on Stackoverflow
Solution 1 - AndroidthetestspecimenView Answer on Stackoverflow
Solution 2 - AndroidKrishnaView Answer on Stackoverflow
Solution 3 - AndroidVV WView Answer on Stackoverflow
Solution 4 - AndroidLuvnish MongaView Answer on Stackoverflow
Solution 5 - AndroideytschkayView Answer on Stackoverflow
Solution 6 - AndroidSaba-21View Answer on Stackoverflow
Solution 7 - AndroidCarlos MionView Answer on Stackoverflow
Solution 8 - AndroidTaras VovkovychView Answer on Stackoverflow
Solution 9 - AndroidNarmiView Answer on Stackoverflow
Solution 10 - AndroidPerracoLabsView Answer on Stackoverflow