MerlinBG
MerlinBG

Reputation: 348

How can I detect screen rotation?

is it possible to detect screen rotation? I mean - rotation only, which is clearly distinguishable from activity initialization from another activity?

The onXxx methods seem not to be useful for this, I have tried adding/removing a flag from the starting Intent (the removing seems not to be reflected, on rotate the flag is there), and have tried adding android:configChanges="orientation" for the activity in the manifest, however the onConfigurationChanged method seems to be called every second rotation... wired.

I guess I am missing something... but haven't found clear solution in the other related threads.

Any ideas?

Upvotes: 18

Views: 32555

Answers (3)

Akexorcist
Akexorcist

Reputation: 2297

Use onConfigurationChanged method to detect the screen rotation isn't good idea. Configuration Change in this activity wouldn't works correctly when user rotate the screen.

So I use this solution to solve this problem.

public class SampleActivity extends AppCompatActivity {
    public static final String KEY_LAST_ORIENTATION = "last_orientation";
    private int lastOrientation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        if (savedInstanceState == null) {
            lastOrientation = getResources().getConfiguration().orientation;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkOrientationChanged();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
    }

    private void checkOrientationChanged() {
        int currentOrientation = getResources().getConfiguration().orientation;
        if (currentOrientation != lastOrientation) {
            onScreenOrientationChanged(currentOrientation);
            lastOrientation = currentOrientation;
        }
    }

    public void onScreenOrientationChanged(int currentOrientation) {
        // Do something here when screen orientation changed
    }
}

But code still not good enough to use it in my project, so I applied this code to my own library (https://github.com/akexorcist/ScreenOrientationHelper).

compile 'com.akexorcist:screenorientationhelper:<latest_version>'

You can create base activity class like this

    public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
    private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helper.onCreate(savedInstanceState);
        helper.setScreenOrientationChangeListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        helper.onStart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        helper.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        helper.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onScreenOrientationChanged(int orientation) {

    }
}

Then extend the activity with this base class

public class MainActivity extends BaseActivity {

    ...

    @Override
    public void onScreenOrientationChanged(int orientation) {
        // Do something when screen orientation changed
    }
}

Done!

Upvotes: 8

kannappan
kannappan

Reputation: 2250

Manifest:

<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>

Activity:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    Log.d("tag", "config changed");
    super.onConfigurationChanged(newConfig);

    int orientation = newConfig.orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT)
        Log.d("tag", "Portrait");
    else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        Log.d("tag", "Landscape");
    else
        Log.w("tag", "other: " + orientation);

    ....
}

try this link also

How do I detect screen rotation

Upvotes: 25

Finuka
Finuka

Reputation: 730

Why don't you try this?

in onCreated get the orientation of the phone:

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
myOrientation = display.getOrientation();

then, override the method onConfigurationChanged and check if the orientation has changed:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if(newConfig.orientation != myOrientation)
        Log.v(tag, "rotated");
    super.onConfigurationChanged(newConfig);
}

Don't forget to add into the manifest android:configChanges="orientation" in the activity.

Upvotes: 1

Related Questions