Android determine screen orientation at runtime

后端 未结 6 727
清酒与你
清酒与你 2021-02-01 05:41

Here\'s a pseudo code to detect screen rotate event, and decide to retain or changes the screen orientation.

public boolean onOrientationChanges(orientation) {
          


        
6条回答
  •  再見小時候
    2021-02-01 06:11

    You don't need to intercept the event and then override it. Just use:

    // Allow rotation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    // Lock rotation (to Landscape)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
    

    Points to note here are, if on Jellybean and above this will allow a 180 degree rotation when locked. Also when unlocked this only allows rotation if the user's master settings is to allow rotation. You can forbid 180 degree rotations and override the master settings and allow rotation, and much much more, so check out the options in ActivityInfo

    In addition, if you have pre-set that there is to be no rotation, then your activity will not be destroyed and then restarted, just for you to set the orientation back which will again cause the activity to be restarted; Thus setting what you want in advance can be much more efficient.

    Pre Jellybean use ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE -- no 180 degree rotation with this.

提交回复
热议问题