(Android)Activity生命周期详解(原创)
1.先来张官方文档的生命周期图
2.下面我们用代码解释Activity生命周期
FirstActivity.java
package mars.activity04; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FirstActivity extends Activity implements OnClickListener{ private Button btn1; private Button btn2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); btn1 = (Button)findViewById(R.id.Btn1); btn2 = (Button)findViewById(R.id.Btn2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); System.out.println("FirstAcvity-------------------------->onCreate"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("FirstAcvity-------------------------->onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("FirstAcvity-------------------------->onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("FirstAcvity-------------------------->onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("FirstAcvity-------------------------->onStop"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("FirstAcvity-------------------------->onRestart"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("FirstAcvity-------------------------->onDestroy"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.Btn1: Intent intent = new Intent(); intent.setClass(this,SecondActivity.class); startActivity(intent); break; case R.id.Btn2: this.finish(); System.out.println("FirstAcvityfinish-------------------------->"); break; } } }
SecondActivity.java
public class SecondActivity extends Activity implements OnClickListener { private Button btn1; private Button btn2; private Button btn3; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); btn1 = (Button)findViewById(R.id.backbtn); btn2 = (Button)findViewById(R.id.thirdButton); btn3 = (Button)findViewById(R.id.finish2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); System.out.println("SecondActivity-------------------------->onCreate"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("SecondAcvity-------------------------->onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("SecondAcvity-------------------------->onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("SecondAcvity-------------------------->onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("SecondAcvity-------------------------->onStop"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("SecondAcvity-------------------------->onRestart"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("SecondAcvity-------------------------->onDestroy"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.backbtn: Intent intent1 = new Intent(); intent1.setClass(this,FirstActivity.class); startActivity(intent1); break; case R.id.thirdButton: Intent intent2 = new Intent(); intent2.setClass(this,ThirdActivity.class); startActivity(intent2); break; case R.id.finish2: this.finish(); System.out.println("SecondAcvityfinish-------------------------->"); break; } } }
ThirdActivity.java
public class ThirdActivity extends Activity implements OnClickListener{ private Button btn; private Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.third); btn=(Button)findViewById(R.id.backToSecondButton); btn2=(Button)findViewById(R.id.finish3); btn.setOnClickListener(this); btn2.setOnClickListener(this); System.out.println("ThirdAcvity-------------------------->onCreate"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("ThirdAcvity-------------------------->onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("ThirdAcvity-------------------------->onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("ThirdAcvity-------------------------->onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("ThirdAcvity-------------------------->onStop"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("ThirdAcvity-------------------------->onRestart"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("ThirdAcvity-------------------------->onDestroy"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.backToSecondButton: Intent intent=new Intent(); intent.setClass(this, SecondActivity.class); startActivity(intent); break; case R.id.finish3: this.finish(); System.out.println("ThirdAcvityfinish-------------------------->"); break; } }
运行结果:
启动程序FirstAcvity:
FirstAcvity-------------------------->onCreate
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
启动SecondAcvity:(注意onPause和onStop是分开的)
FirstAcvity-------------------------->onPause
SecondActivity----------------------->onCreate
SecondAcvity------------------------->onStart
SecondAcvity------------------------->onResume
FirstAcvity-------------------------->onStop
启动ThirdAcvity:(ThirdAcvity是Dialog类型的Activity)
SecondAcvity------------------------->onPause
ThirdAcvity-------------------------->onCreate
ThirdAcvity-------------------------->onStart
ThirdAcvity-------------------------->onResume
返回SecondAcvity:(Intent就是Create Activity)
ThirdAcvity-------------------------->onPause
SecondAcvity------------------------->onStop
SecondActivity----------------------->onCreate
SecondAcvity------------------------->onStart
SecondAcvity------------------------->onResume
ThirdAcvity-------------------------->onStop
返回FirstAcvity:
SecondAcvity-------------------------->onPause
FirstAcvity--------------------------->onCreate
FirstAcvity--------------------------->onStart
FirstAcvity--------------------------->onResume
SecondAcvity-------------------------->onStop
FirstAcvity下按home键:(正常的变得不可见)
FirstAcvity--------------------------->onPause
FirstAcvity--------------------------->onStop
重新回到FirstAcvity:(正常的操控Activity)
FirstAcvity--------------------------->onRestart
FirstAcvity--------------------------->onStart
FirstAcvity--------------------------->onResume
ThirdAcvity下按home键:
ThirdAcvity-------------------------->onPause
SecondAcvity-------------------------->onStop
ThirdAcvity-------------------------->onStop
重新回到ThirdAcvity
SecondAcvity-------------------------->onRestart
SecondAcvity-------------------------->onStart
ThirdAcvity-------------------------->onRestart
ThirdAcvity-------------------------->onStart
ThirdAcvity-------------------------->onResume
FirstAcvity下熄屏:(相当于锁屏来到前端Another Activity,相当于FirstAcvity还可见)
FirstAcvity-------------------------->onPause
亮屏解锁到FirstAcvity:(返回)
FirstAcvity-------------------------->onResume
FirstAcvity下另一个app的Activity来到前端:(正常的变得不可见)
FirstAcvity-------------------------->onPause
FirstAcvity-------------------------->onStop
回到FirstAcvity:(正常的操控Activity)
FirstAcvity-------------------------->onRestart
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
FirstAcvity中调用finish():
FirstAcvityfinish-------------------------->
FirstAcvity-------------------------->onPause
FirstAcvity-------------------------->onStop
FirstAcvity-------------------------->onDestroy
SecondAcvity中调用finish():
SecondAcvityfinish-------------------------->
SecondAcvity-------------------------->onPause
FirstAcvity-------------------------->onRestart
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
SecondAcvity-------------------------->onStop
SecondAcvity-------------------------->onDestroy
ThirdAcvity中调用finish():
ThirdAcvityfinish-------------------------->
ThirdAcvity-------------------------->onPause
SecondAcvity-------------------------->onResume
ThirdAcvity-------------------------->onStop
ThirdAcvity-------------------------->onDestroy
关闭程序后重新启动FirstActivity:
...
启动SecondActivity:
...
启动ThirdActivity:
...
按Back键,返回SecondActivity:
ThirdAcvity-------------------------->onPause
SecondAcvity-------------------------->onResume
ThirdAcvity-------------------------->onStop
ThirdAcvity-------------------------->onDestroy
按Back键,返回FirstActivity:
SecondAcvity-------------------------->onPause
FirstAcvity-------------------------->onRestart
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
SecondAcvity-------------------------->onStop
SecondAcvity-------------------------->onDestroy
按Back键,返回桌面:
FirstAcvity-------------------------->onPause
FirstAcvity-------------------------->onStop
FirstAcvity-------------------------->onDestroy
finish说明:对于finish,调用finish后,不会立即调用onDestroy,
而是在执行完finish后的代码后调用onPause,onStop,onDestroy;
4.Activity栈:
ps:调用后create,所属Activity会进栈;调用了onDestroy,所属Activity会出栈;前端Activity总是在栈顶
使用以下操作:
启动1启动SecondActivity -> 2返回一个Activity -> 1finish-> 2finish -> 1finish
栈情况:
运行结果:
1.启动 FirstAcvity:
FirstAcvity-------------------------->onCreate
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
2.启动 SecondActivity:
FirstAcvity-------------------------->onPause
SecondActivity-------------------------->onCreate
SecondAcvity-------------------------->onStart
SecondAcvity-------------------------->onResume
FirstAcvity-------------------------->onStop
3.返回第一个Activity:
SecondAcvity-------------------------->onPause
FirstAcvity-------------------------->onCreate
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
SecondAcvity-------------------------->onStop
4.
FirstAcvityfinish-------------------------->
FirstAcvity-------------------------->onPause
SecondAcvity-------------------------->onRestart
SecondAcvity-------------------------->onStart
SecondAcvity-------------------------->onResume
FirstAcvity-------------------------->onStop
FirstAcvity-------------------------->onDestroy
5.
SecondAcvityfinish-------------------------->
SecondAcvity-------------------------->onPause
FirstAcvity-------------------------->onRestart
FirstAcvity-------------------------->onStart
FirstAcvity-------------------------->onResume
SecondAcvity-------------------------->onStop
SecondAcvity-------------------------->onDestroy
6.
FirstAcvityfinish-------------------------->
FirstAcvity-------------------------->onPause
FirstAcvity-------------------------->onStop
FirstAcvity-------------------------->onDestroy
由以上知,转到某一个Activity,必然会调用onResume方法,故这个Activity的一些初始化工作一般放到onResume里面。
现在Activity生命周期应该理解得差不多了,如有错误,欢迎指正。