为了不让activity在发生横竖屏切换时生命周期不要改变,
需要在配置文件中添加
android:configChanges="keyboardHidden|orientation|screenSize"<activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenSize"/> 添加了这句代码后,在发生横竖屏切换时activity 的生命周期不会改变 下面的这个方法才会有效@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Configuration cfg = getResources().getConfiguration(); if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "横屏", Toast.LENGTH_SHORT).show(); } else if (cfg.orientation == Configuration.ORIENTATION_PORTRAIT) { Toast.makeText(this, "竖屏", Toast.LENGTH_SHORT).show(); } }