Seifallah Azzabi
Seifallah Azzabi

Reputation: 140

Allow / Deny screen rotation in onConfigurationChange() - Android

I have an activity that handles 2 modes of display :

  1. Normal mode : The screen should not rotate, always in portrait mode.
  2. Fullscreen mode : The screen could be portrait or landscape

I have only 1 activity to handle the 2 modes with :

  1. boolean variable isFullscreen() that indicates either the Activity is in fullscreen mode or normal mode.
  2. changeMode() calls setContentView() to switch the XML layout proper to the current mode
  3. in the manifest file, i have android:configChanges="screenSize|orientation" to let the Activity handle configuration change
  4. overriding the method onConfigurationChanged().

I've tried this :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if (isFullscreen) {
        super.onConfigurationChanged(newConfig);
    } else {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

but in this case : the screet does not rotate in any mode (not even in fullscreen mode, with isFullscreen() et to true)

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if (isFullscreen) {
        super.onConfigurationChanged(newConfig);
    } else {

    }
}

But I got Super Not Called exception or something like that (I was thinking if i don't call super in the case of normal mode, it would not rotate the screen)

And finally, I've tried this :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if (isFullscreen) {
        super.onConfigurationChanged(newConfig);
    } else {
        newConfig.orientation = Configuration.ORIENTATION_PORTRAIT;
        super.onConfigurationChanged(newConfig);
    }
}

Upvotes: 2

Views: 1392

Answers (1)

mstrengis
mstrengis

Reputation: 839

when you are toggling fullscreen call this don't do it in onConfigurationChanged

code:

if(isFullscreen){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}else{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Upvotes: 2

Related Questions