KyluAce
KyluAce

Reputation: 1000

Android app switch to windowed mode after minimizing and changing device orientation

I’m creating an app where the user can change the application’s orientation, but the auto-rotate feature will be disabled.

The expected behavior is as follows:

1)The user goes to the settings. 2)The user selects, for example, landscape mode. 3)The app remains in landscape mode at all times (of course, the user can go to settings and change it to portrait mode).

Depending on the switch, I set the requestedOrientation to either portrait or landscape mode, and then lock the orientation. It looks like this:

public void rotate(boolean landscape) {
    if (landscape)
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    else
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}

In other activities, I check the current orientation against the switch value, and if they are not the same, I call a rotation function:

public void checkRotation(boolean landscape) {
    if(landscape && getResources().getConfiguration().orientation == 1)
        rotate(true);
    else if(!landscape && getResources().getConfiguration().orientation == 2)
        rotate(false);
}

The issue is that it doesn’t work correctly when the app is minimized, device is rotated and then we back to the app. Then it looks like this:

enter image description hereenter image description here

So my app orientation is good, but suddenly it's displayed in windowed mode (app doesn't support windowed mode).

I need to say that when I call rotate function in each activity without checking if orientation is in good position it worked but it's not optimal solution to call it all the time.

How to fight this? Or maybe you have better solution for handling orientation?

Upvotes: 0

Views: 122

Answers (0)

Related Questions