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

前端 未结 10 1686
迷失自我
迷失自我 2020-12-01 03:35

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 a

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 03:49

    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);
            }
        }
    }
    

提交回复
热议问题