Action created twice when setRequestedOrientation () is called to display in landscape mode

I have an Android app, I want to make the tablet (sw600dp and higher) display in landscape mode and the phone to display in portrait mode, so I call onCreate of my BaseActivity class

boolean isTablet= getResources().getBoolean(R.bool.isTablet);
if (isTablet) {
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} 

And how I put "isTablet" in the bools.xml file and put it in

values for phone

   <resources>
        <bool name="isTablet">false</bool>
   </resources>

values-sw600dp for tablet

   <resources>
        <bool name="isTablet">true</bool>
   </resources>

And in AndroidManifest I use

android:screenOrientation="nosensor"

just unplug the device’s orientation sensor.

It seems that my approach works fine (Landscape for tablet and Portrait for phone), but the problem arises when I launch my application on Nexus 7 - my actions are created twice . These steps are:

  • Nexus 7 ( , )

, setRequestedOrientation() ( 2 ). . AndroidManifest, :

android:screenOrientation="@integer/orientation"

: SCREEN_ORIENTATION_LANDSCAPE = 0 SCREEN_ORIENTATION_PORTRAIT = 1

"" integers.xml

<resources>
    <integer name="orientation">1</integer>
</resources>

-sw600dp

<resources>
    <integer name="orientation">0</integer>
</resources>

, , , AndroidMenifest "" , values-sw600dp. , 2 . ? ? .

+6
2

setRequestedOrientation(int):

, (, , )

, , .

, , onCreate, .

+1

, .

, , Activity onCreate :

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

, ( ).

Activity , , , .

, , Activity . setRequestedOrientation ( ).


.. .

  • setRequestedOrientation , onSaveInstanceState onRestoreInstanceState, , ..
  • , - , setRequestedOrientation, .

    .

    , :

    : configChanges = " | | keyboardHidden"

    , . - This technique should be considered a last resort

    , :

       private void rotateScreen(Uri vidUri) {
           try {
               MediaMetadataRetriever retriever = new MediaMetadataRetriever();
               Bitmap bmp;
               retriever.setDataSource(this, vidUri);
               bmp = retriever.getFrameAtTime();
    
               videoWidth = bmp.getWidth();
               videoHeight = bmp.getHeight();
    
               if (videoWidth > videoHeight) {
                   setContentView(R.layout.activity_video_player_land);
                   this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
               }
               if (videoWidth < videoHeight) {
                   setContentView(R.layout.activity_video_player);
                   this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
               }
    
           } catch (RuntimeException ex) {
               Log.e("MediaMetadataRetriever", "- Failed to rotate the screen");
    
           }
       }
    

    , , , , .


, , ( ), , , .

0

All Articles