对于Activity生命周期几个主要的属性
public void onCreate() 打开一个界面,创造界面
public void onStart() 开始这个界面
public void onResume() 用户操作界面,产生交互时运行
public void onPause() 暂停界面
public void onStop() 停止界面
public void onDestroy() 销毁界面
现在我们来看一个实例,先建一个Activity,如下
public class MainActivity extends AppCompatActivity {
//private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
对界面上的按钮进行监听 让其跳转画面
*bt=(Button)findViewById(R.id.bt1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); //跳转画面
startActivity(intent);
}
}
);*/
Log.d("====MainActivity=","onCreate"); //与手机连接时,可以进行查询运行状态"====MainActivity="查询关键词 "onCreate" 提示信息
}
@Override
protected void onStart(){
super.onStart();
Log.d("====MainActivity=", "onStart");
}
@Override
protected void onRestart(){
super.onRestart();
Log.d("====MainActivity=", "onRestart");
}
@Override
protected void onResume(){
super.onResume();
Log.d("====MainActivity=", "onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.d("====MainActivity=", "onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.d("====MainActivity=", "onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("====MainActivity=", "onDestroy");
}
}
当你运行该app,并打开界面时运行的顺序是
====MainActivity=:onCreate
====MainActivity=: onStart
====MainActivity=: onResume//创建,开始,交互
home键返回桌面
====MainActivity=: onPause
====MainActivity=: onStop
再次进入
====MainActivity=: onRestart
====MainActivity=: onStart
====MainActivity=: onResume
直接退出键退出
====MainActivity=: onPause
====MainActivity=: onStop
====MainActivity=: onDestroy
现在我们在建立一个SecondActivity,可以从MainActivity跳转到该activity,即运行Mainactivity中注释掉的代码。SecondActivity代码如下
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
protected void onStart(){
super.onStart();
Log.d("===M==","onStart");
}
@Override
protected void onResume(){
super.onResume();
Log.d("===M==","onResum");
}
@Override
protected void onRestart(){
super.onRestart();
Log.d("===M==","onRestart");
}
@Override
protected void onPause(){
super.onPause();
Log.d("===M==","onPaues");
}
@Override
protected void onStop(){
super.onStop();
Log.d("===M==","onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d("===M==","onDestory");
}
}
当我们进行跳转时
====MainActivity=: onPause
===M==: onStart
===M==: onResum
====MainActivity=: onStop
返回MainActivity界面
===M==: onPaues
====MainActivity=: onRestart
====MainActivity=: onStart
====MainActivity=: onResume
===M==: onStop
===M==: onDestory