安卓学习,Activity生命周期
2017-11-25 本文已影响0人
天青色等烟雨hhft
代码:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LifeCycleActivity";
private Context context = this;
/**
* Activity创建时被调用
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreat()");
setContentView(R.layout.activity_main);
TextView textView= (TextView) findViewById(R.id.tv);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context,Activity2.class));
}
});
}
/**
* Activity创建或者从其他Activity返回时被调用
* */
@Override
protected void onStart() {
super.onStart();
Log.i(TAG,"onStart()");
}
/**
* 从其他Activity返回时被调用
* 执行了onStop()方法但是没有执行onDestroy()时再次回到Activity才会被调用
* */
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG,"onRestart()");
}
/**
* Activity创建或者从被覆盖、后台重新回到前台时被调用
* */
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"onResume()");
}
/**
*Activity被覆盖或者退出程序时被调用
* */
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"onPause()");
}
/**
* 当前Activity被覆盖或者退出时被调用
* */
@Override
protected void onStop() {
super.onStop();
Log.i(TAG,"onStop()");
}
/**
* 退出Activity时被调用
**/
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"onDestroy()");
}
}
运行结果:
进入应用

跳转到另外一个Activity

按返回键回到此Activity

锁屏

解锁
