How can I detect screen rotation?

前端 未结 3 1089
轻奢々
轻奢々 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

    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:'
    

    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!

提交回复
热议问题