|
本帖最后由 liu 于 2018-12-10 15:47 编辑
Activity的生命周期
Android新手开发之旅【目录】
先来看一下activity的生命周期流程图:
由图可见一个最简单完整的生命周期流程顺序是:onCreate-->onStart- -> onResume --> onPause --> onStop --> onDestroy
1、当启动Activity时先调用onCreate方法
2、当Activity处于可见状态时调用onStart方法
3、当Activity可以得到用户焦点时调用onResume方法,Activity进入运行状态
4、当Activity被其他Activity覆盖或被锁屏时调用onPause方法,Activity进入暂停状态
5、当Activity由被覆盖状态回到前台或解锁屏时调用onResume方法,Activity会再次进入运行状态
6、当Activity处于不可见状态的时调用onStop方法,Activity进入停滞状态
7、当再次回到此Activity时先调用onRestart方法,然后调用onStart方法,最后调用onResume方法,Activity再次进入运行状态
8、当Activity处于不可见状态或者被覆盖时,内存不足会杀死当前Activity,当用户回到当前Activity时会重新调用onCreate方法、onStart方法、onResume方法,进入运行状态
接下来看一下两个Activity之间跳转时它们的生命周期(MainActivity和SecondActivity):
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("ActivityStatus", "MainActivity-----onCreate");
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Activity之间的跳转
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i("ActivityStatus", "MainActivity-----onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i("ActivityStatus", "MainActivity-----onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i("ActivityStatus", "MainActivity-----onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i("ActivityStatus", "MainActivity-----onStop");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("ActivityStatus", "MainActivity-----onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("ActivityStatus", "MainActivity-----onDestroy");
}
}
SecondActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.i("ActivityStatus", "SecondActivity-----onCreate");
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i("ActivityStatus", "SecondActivity-----onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i("ActivityStatus", "SecondActivity-----onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i("ActivityStatus", "SecondActivity-----onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i("ActivityStatus", "SecondActivity-----onStop");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("ActivityStatus", "SecondActivity-----onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("ActivityStatus", "SecondActivity-----onDestroy");
}
}
布局代码就不贴了,就一个Button按钮,操作流程是先进入MainActivity,然后点击MainActivity中的按钮跳转到SecondActivity,接下来点击SecondActivity中的按钮结束掉SecondActivity返回到MainActivity中,看一下打印出来的日志:
执行方法顺序:
1、首先是进入MainActivity,依次执行onCreate-->onStart- -> onResume
2、点击MainActivity中按钮后进行跳转,首先执行MainActivity中的onPause ,然后依次执行SecondActivity中的onCreate-->onStart- -> onResume方法,最后执行MainActivity中的onStop方法
3、点击SecondActivity中的按钮结束SecondActivity返回MainActivity时,先执行SecondActivity中的onPause 方法,然后依次执行MainActivity中的onRestart-->onStart-->onResume,最后执行SecondActivity中的onStop和onDestroy方法
|
|