How can I detect screen rotation?

前端 未结 3 997
轻奢々
轻奢々 2021-01-17 10:25

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

The onXxx meth

3条回答
  •  一向
    一向 (楼主)
    2021-01-17 10:51

    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.

提交回复
热议问题