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生命周期应该理解得差不多了.

时间: 2024-11-19 23:17:37

Android的Activity生命周期(原)的相关文章

Android之Activity生命周期浅析(一)

??Activity作为四大组件之一,出现的频率相当高,基本上我们在android的各个地方都能看见它的踪影,因此深入了解Activity,对于开发高质量应用程序是很有帮助的.今天我们就来详细地聊聊Activity的生命周期,以便我们在以后的开发中能如鱼得水. 一.初识Activity ??在日常应用中Activity是与用户交互的接口,它提供了一个用户完成相关操作的窗口.当我们在开发中创建Activity后,通过调用setContentView(View)方法来给该Activity指定一个布局

【Android】Activity生命周期(亲测)

测试手机:Nexus 5   系统:4.4 一.测试 测试代码: 1 package com.example.androidalarm; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.res.Configuration; 6 import android.os.Bundle; 7 import android.util.AttributeSet; 8 impo

Android开发--Activity生命周期回顾理解

Activity和Servlet一样,都用了回调机制.我们通过类比servlet来学习Activity.当一个servlet开发出来之后,该servlet运行于Web服务器中.服务器何时创建servlet的实例,何时调用servlet的方法向用户生成响应,程序员无法控制,这种回调由服务器自行决定.Activity也一样,被开发出来,开发者只要在AndroidManifest.xml文件配置该Activity即可.至于该Activity何时被实例化,它的方法何时被调用,对开发者来说完全是透明的.

Android之Activity生命周期的浅析(二)

??上一篇文章,我们主要分析了Activity的正常情况下生命周期及其方法,本篇主要涉及内容为Activity的异常情况下的生命周期. Activity异常生命周期 ??异常的生命周期是指Activity被系统回收或者当前设备的Configuration发生变化(一般指横竖屏切换)从而导致Activity被销毁重建.异常的生命周期主要分以下两种情况: 1.相关的系统配置发生改变导致Activity被杀死并重新创建(一般指横竖屏切换) 2.内存不足导致低优先级的Activity被杀死 1.相关的系

大话Android 之activity生命周期

众所周知,Android四大组件里,最重要的就是activity了.而作为开发者,我们最应该关注的,当然少不了activity的生命周期啦. 说到activity生命周期,最好的方式当然是通过流程图来解释啦,下面是官方给的图: 这个一堆e文的流程图,怎么理解呢?可以理解为一部狗血的宫斗剧. activity就像一个秀女一样,整个生命周期里充满了争斗与起伏. "小秀女"activity的一生有四个状态: 1."后宫佳丽三千人,三千宠爱在一身"(活动状态):住在毓秀宫(

浅谈Android之Activity生命周期

 Activity作为四大组件之一,出现的频率相当高,基本上我们在android的各个地方都能看见它的踪影,因此深入了解Activity,对于开发高质量应用程序是很有帮助的.今天我们就来详细地聊聊Activity的生命周期,以便我们在以后的开发中能如鱼得水. 一.初识Activity   在日常应用中Activity是与用户交互的接口,它提供了一个用户完成相关操作的窗口.当我们在开发中创建Activity后,通过调用setContentView(View)方法来给该Activity指定一个布局界

【Android】Activity 生命周期具体解释

与其它编程模式不同,android中的Activity没有main()函数.我们无法决定Activity的创建和销毁过程,Activiy的创建和销毁(即生命周期)由系统完毕,系统会在Activity的生命周期的各个阶段回调Activity对应的成员函数. 假设你想在Activity的某个生命周期阶段完毕一些事情,你要做的就是重写(Override)对应的生命周期回调函数,并在该函数中完毕对应的任务.Activity的生命周期函数有onCreate().onStarted().onResumed(

android开发 - Activity生命周期

本章主讲Activity的生命周期 Activity整个生命周期有四种状态,7个方法,3个嵌套循环 状态: 1.  Activity/Running 活动状态 指的是当前用户正在交互的activity状态 2. Paused                  暂停状态 指的是当前activity可见,但是被吐司或者对话框遮罩时状态 3. Stopped                停止状态 指的是被新的创建的activity遮挡时的状态,它保留了之前的操作信息和状态,但是如果系统内存不够,可能

【Android】Activity生命周期研究

1. 为什么研究Activity什么周期 Activity是Android四大组件之一,是Android人必须要深刻理解的基础内容之一. 可以提高我们对相关问题的解决能力. 对一个问题如果不能深入理解,总觉得不踏实. 2. Activity生命周期详解 2.1 Activity生命周期图解 下图是Android开发官网的原图,清晰表达了Activity生命周期中每个方法所处位置及调用流程. 上图对Activity的执行流程表达很到位,但是对每个方法在执行时,我们的设备处于一种什么状态,其中注释表