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式返回就是onCreate)
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

 

对于finish,调用finish后,不会立即调用onDestroy,

而是在执行完finish后的代码后调用onPause,onStop,onDestroy;

 

ps:调用后create,所属Activity会进栈;调用了onDestroy,所属Activity会出栈;

使用以下操作:

 1启动SecondActivity     ->     2返回一个Activity     ->     1finish->     2finish      ->     1finish

 

栈情况:

技术分享

运行结果:

 FirstAcvity-------------------------->onCreate
FirstAcvity-------------------------->onStart 

FirstAcvity-------------------------->onResume
FirstAcvity-------------------------->onPause
 SecondActivity-------------------------->onCreate
 SecondAcvity-------------------------->onStart
 SecondAcvity-------------------------->onResume
 FirstAcvity-------------------------->onStop
 SecondAcvity-------------------------->onPause
 FirstAcvity-------------------------->onCreate
 FirstAcvity-------------------------->onStart
 FirstAcvity-------------------------->onResume
SecondAcvity-------------------------->onStop
 FirstAcvityfinish-------------------------->
 FirstAcvity-------------------------->onPause
 SecondAcvity-------------------------->onRestart
 SecondAcvity-------------------------->onStart
 SecondAcvity-------------------------->onResume
FirstAcvity-------------------------->onStop
FirstAcvity-------------------------->onDestroy
 SecondAcvityfinish-------------------------->
 SecondAcvity-------------------------->onPause
FirstAcvity-------------------------->onRestart
 FirstAcvity-------------------------->onStart
 FirstAcvity-------------------------->onResume
 SecondAcvity-------------------------->onStop
 SecondAcvity-------------------------->onDestroy
 FirstAcvityfinish-------------------------->
 FirstAcvity-------------------------->onPause
FirstAcvity-------------------------->onStop
 FirstAcvity-------------------------->onDestroy

 

 

现在Activity生命周期应该理解得差不多了.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。