57

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

Sol
  • 833
  • 1
  • 9
  • 17
  • 2
    Still nothing new ? I'm also experiencing the bug, but in my case, it happens in an app that is completely in portrait mode and only when activities that have been called with startActivityForResult() return their result (so we go back to the caller activity). – lcw_gg Jan 09 '18 at 03:26
  • Do you solve the problem ? I have same issue. – Wun Mar 13 '18 at 03:17
  • Nope, there is not solution. It seems that the bug that I reported is blocked by other issue that we don't have access. https://issuetracker.google.com/issues/69168442 – Sol Mar 13 '18 at 14:02
  • I'm facing this problem with my device, but have a little different in behavior. Still there's no working solution. https://stackoverflow.com/questions/49232198/screen-rotate-3-times-when-back-to-landscape-activity-from-portrait-activity – quangkid Mar 15 '18 at 04:03
  • I have the exact same problem in my app as well – Aniruddha K.M Apr 07 '18 at 20:18
  • We are facing the same issue - just that we can't modify the landscape activity as described below. Any solutions yet? – Tobias Reich Jul 20 '18 at 12:00
  • facing same weird issue – Smeet Jul 29 '18 at 18:36
  • I have the same problem, with a landscape video activity.. – Rony Tesler Aug 15 '18 at 00:45

10 Answers10

16

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.

thetestspecimen
  • 328
  • 2
  • 11
  • Thanks, but explain - When to use the setRequestedOrientation(...) method - before the super.onPause(), or after ? And the same for onResume() – Taras Vovkovych Apr 04 '18 at 18:00
  • In this case it makes no difference (I just tested it to make sure). I have personally called the methods after the super, as this was the most logical to me. If there was anything in the super call that was causing the glitch then the code would need to be applied after the super. – thetestspecimen Apr 05 '18 at 18:28
  • Using: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK) in previous Activity resolves my issue. – Luvnish Monga Apr 20 '18 at 06:06
  • @LuvnishMonga I tried the same .But still i am getting Orientation Issue.I started my activity with that flags.Or its greatly appreciationable by sending any code snippet – VV W Jun 21 '18 at 06:03
  • If i place setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); this line on oncreate i am getting exception Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation – VV W Jun 21 '18 at 06:27
10

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);
}
Krishna
  • 462
  • 1
  • 5
  • 6
3

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

VV W
  • 169
  • 7
1

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

Luvnish Monga
  • 7,072
  • 3
  • 23
  • 30
  • I tried the same .But still i am getting Orientation Issue.I started my activity with that flags.Or its greatly appreciationable by sending any code snippet – VV W Jun 21 '18 at 06:03
  • yes, I got this issue in Oreo and this is the solution which I found for Oreo. – Luvnish Monga Jun 21 '18 at 11:41
  • ************* SOLVED ************************************ If u place the DialogTheme to activity remove the set orientation from the activity tag , It will take the Orientation from the previous activity . – VV W Jun 21 '18 at 12:50
  • @RaisingDev Please share the answer – Ahmad Arslan Jun 27 '18 at 13:05
  • @AhmadArslan If u place the DialogTheme to activity remove the set orientation from the activity tag which is from manifesto , It will take the Orientation from the previous activity and for below Versions place this on oncreate if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } – VV W Jun 27 '18 at 13:11
  • @AhmadArslan Actually For android 8.0, the previous activity Orientation decides the current activityOrientation for dialogtheme based activities.. – VV W Jun 27 '18 at 13:14
  • You mean to say instead of using "parent="Theme.AppCompat.Light" I have to use "parent="Theme.AppCompat..Dialog" theme in the manifest and remove all the orientation from the Manifest file and use orientation programmatically ??? – Ahmad Arslan Jun 27 '18 at 13:15
  • @AhmadArslan No ..,If u have "parent="Theme.AppCompat..Dialog" from manifesto ,for that only u have to remove Orientation tag from manifesto ,and put orientation for remaing activites that wil work. place Dialog oncreate if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } – VV W Jun 27 '18 at 13:20
1

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.

eytschkay
  • 961
  • 6
  • 9
1

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();
}
Saba-21
  • 132
  • 1
  • 9
0

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

Carlos Mion
  • 37
  • 1
  • 8
  • 1
    I already put this on the manifest and is working for all android versions except 8.1 . – Sol Nov 10 '17 at 17:47
  • Try it in onResume-- your activity may not be destroyed, so onCreate() might not be called at that particular point. – Will Mar 01 '18 at 19:43
0

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.

Taras Vovkovych
  • 4,062
  • 2
  • 16
  • 21
0

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();
}
Narmi
  • 48
  • 9
0

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);
        }
    }
}
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127