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

前端 未结 10 1495
迷失自我
迷失自我 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 04:03

    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.

    0 讨论(0)
  • 2020-12-01 04:04

    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.

    0 讨论(0)
  • 2020-12-01 04:04

    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();
    }
    
    0 讨论(0)
  • 2020-12-01 04:13

    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.

    0 讨论(0)
提交回复
热议问题