我们发现如果activity从竖屏到横屏,activity的生命周期会杀死然后在创建,当从横屏到竖屏是,activity的生命周期会有两次从杀死到创建,
原因是输入法的存在。如何使他们一样
可以在AndroidMenifest.xml的Activity节点配置上android:configChanges="keyboardHidden"
为了节省内存,可以这样配置,是它们的生命周期重用
android:configChanges="keyboardHidden|orientation"
如果配置上Android:screenOrientation="landscape"
屏幕会固定
还有一种情况,在onConfigurationChanged方法里设置不同方向显示不同的布局
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//得到方向属性
int orientation = newConfig.orientation;
if(orientation == Configuration.ORIENTATION_LANDSCAPE){//水平
setContentView(R.layout.other);
}else if(orientation == Configuration.ORIENTATION_PORTRAIT){//垂直
setContentView(R.layout.main);
}
}