activity生命周期:
实例代码:
public class DemoActivity extends Activity { //1、activity第一次被创建的时候,执行
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("onCreate");
int page = 0;
if(savedInstanceState!=null){
page = savedInstanceState.getInt("page");
System.out.println("重新定位到:"+page+"页");//切换屏幕时,重新获取上次读取到的页码
}
}
//2、界面用户可见的时候,执行
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
//3、界面上的按钮,文本等可被触摸时,执行
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
} @Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("onRestart");
} //4、界面上的按钮,文本等不可被触摸时,执行
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
} //在activity异常退出时,执行,保存信息到Bundle对象中去
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("page", 250);//保存当前读取到的页码
}
//5、activity用户不可见
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("onStop");
} //6、activity被销毁的时候,执行
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
} }
禁用android中activity进行屏幕切换
在AndroidManifest.xml的activity属性位置配置:
android:configChanges="keyboardHidden|orientation"