生命周期:运行状态->(onPause)->暂停->onStop()->停止->完成或销毁->onDestroy()->不存在->启动->onCreate()->停止->onStart()->暂停->onResume()->运行
点击返回会到销毁,旋转屏幕会重新运行
横屏布局加载:右键res新建directory 选择layout-orientation创建横屏布局文件存放,复制原来的xml过来,修改为FrameLayout,增加layout_gravity
使用onSaveInstanceState(Bundle outState)保存由于activity被销毁前的值,以便屏幕旋转后使用。onSaveInstanceState存放的是基本数据类型以及Serializable或Parcelable接口的对象:
重写:
public void onSaveInstanceState(Bundle saveInstanceState){
super.onSaveInstanceState(saveInstanceState);
Log.i(TAG,"onSaveInstanceState");
saveInstanceState.putInt(KEY_INDEX,mCurrentIndex);
}
然后在onCreate中使用:
if(savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
}
Log.e(),Log.w(),Log.d(),Log.v(),
调试另外一种Demo:
try{
question = mQuestionBank[mCurrentIndex];
}catch(ArrayIndexOutOfBoundsException ex){
Log.e("Index was out of bounds",ex);
}