iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程中activity的完整生命周期实例详解
  • 910
分享到

Android编程中activity的完整生命周期实例详解

activityAndroid 2022-06-06 09:06:34 910人浏览 泡泡鱼
摘要

本文实例分析了Android编程中activity的完整生命周期。分享给大家供大家参考,具体如下: android中 activity有自己的生命周期,对这些知识的学习可以帮助

本文实例分析了Android编程中activity的完整生命周期。分享给大家供大家参考,具体如下:

android中 activity有自己的生命周期,对这些知识的学习可以帮助我们在今后写程序的时候,更好的理解其中遇到的一些错误。这篇文章很长,希望不要耽误大家的时间~

今天不会涉及太多关于activity栈的东西,主要说activity自身的生命周期

区分几个概念

1 Activity 官方解释为 “An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the  phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. ”也就是用户用来交互的每一个窗口,用户当前正在进行的一个操作。

2 back-stack  用户通过触摸程序会通过application launcher来启动一个activity A,启动的activity A会被压入栈顶,如果当前的activity A再启动一个新的activity B,那么此时A调用onStop函数,系统会维护这个activity信息.当用户按下back键的时候,back stack会进行一个pop的操作,并且调用A的onResume()  具体的进出栈细节,以后会详细介绍。

3 Tasks  当用户处于某个activi提: Activity A在名称为"TaskOne应用ty的时候,按下HOME键用户返回到launcher,此时,如果用户再触摸新的应用,则新建一个Task,一个back stack就代表一个task.不同程序的activity可以压入同一个栈中,也就是说可以组成一个task,比如你的程序启动了一个系统自带的发短信的activity,给用户的感觉就是发短信好像是你的程序中的一个功能一样。

注释:以上的行为均为系统的默认设置,有两种方式可以改变activity的行为,加入A启动B,一是在B的manifest设置中,改变行为,另一种是在Activity发起的intent中指定要启动的activity设置,其中Intent的优先级要高于manifest.xml文件,并且有些mode在并不是同时都存在于两种方式中。

android的生命周期包括  onCreate onStart onRestart onResume onPause onStop onDestroy ,activity在声明周期中会调用以上方法来执行不同状态对应的操作,下面介绍各个方法的调用时间

onCreate()     次状态下activity不可被终结
Called when the activity is first created. This is where you should do all of your nORMal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

Always followed by onStart().
//当activity第一次被创建的时候调用。你应该在这个方法里进行所有的静态创建,创建views,(通过setContnetView方法)向lists绑定数据等等。如果存在保存的状态的话,该方法也提供给你一个包含activity最近状态的一个bundle。onStart方法总是在此方法之后调用

onRestart()    次状态下activity不可被终结
Called after your activity has been stopped, prior to it being started again.
Always followed by onStart()
//在你的activity停止后被调用,在重新开始之前调用

onResume()    次状态下activity不可被终结
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input Going to it.
Always followed by onPause().
//当activity将被启动与用户交互的时候被调用。此刻你的activity处于activity栈的顶端,用于用户输入,永远///在onPause之后被调用

onPause()    次状态下activity不可被终结 ,android HoneyComb系统除外
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

//当系统即将重新开始以前的activity的时候被调用(不懂,自己的理解是:当当前activity要启动新的activity的时候被调用),典型的应用是用来将还未保存的数据提交到当前的数据,(意思就是保存数据更新),停止animations和其他可能耗费CPU的操作。对此方法的实现必须快速因为下个activity直到此方法返回才会被重新开始。

当activity从back(翻译后台不合适)转到front(与用户交互的状态)的时候,onResume方法会在onPause方法之后被调用

当activity变为不可见的时候,onStop方法会在onPause之后被调用

onStop()     次状态下activity可以被终结
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

//当activity对用户不再可见时被调用,因为另一个activity已经重新开始并且覆盖了当前activity(在栈中)。当有新的activity被启动,或者一个存在的activity重新回到前台状态,又或者当前的activity将被销毁。如果activity要返回前台和用户进行交互则在此方法后调用onReatart方法,如果当前activity要消亡,则onDestroy方法将在此方法后被调用

onDestroy()     次状态下activity可以被终结
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenariOS with the isFinishing() method.

这是当你的activity被消亡时接收到的最后一个方法。调用此方法有两种情形:一是 activity将要完成,可通过调用finish方法实现。而是系统销毁activity的实例来释放空间。可以使用isFinish方法来区别两种情形。这个方法常用在onPause方法中,来判断activity是暂停还是将终止。后面的demo将会演示这个功能。

下图是官网的一个生命周期的演示

好了 看一下我写的一个演示的例子吧,

MainFest.xml


<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="Http://schemas.android.com/apk/res/android" 
   package="uni.activity" 
   android:versionCode="1" 
   android:versionName="1.0"> 
  <uses-sdk android:minSdkVersion="7" /> 
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".ActivityDemoActivity" 
         android:label="@string/app_name"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    <activity android:name=".Activity01" 
         android:label="@string/app_name"> 
    </activity> 
  </application> 
</manifest>

布局文件 main.xml


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
<TextView  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:text="@string/hello" 
  /> 
 <Button  
  android:id="@+id/Button_A" 
  android:text="GO to activity 2" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  /> 
</LinearLayout>

activity01.xml


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
<TextView  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:text="@string/hello" 
  /> 
 <Button  
  android:id="@+id/Button_A" 
  android:text="GO to activity 2" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  /> 
</LinearLayout>

String.xml


<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <string name="hello">Hello World, ActivityDemoActivity!</string> 
  <string name="app_name">ActivityDemo</string> 
  <string name="activity01">this is activity 01</string> 
</resources>

ActivityDemoActivity.java


 
package uni.activity; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
public class ActivityDemoActivity extends Activity { 
   
  private static final String TAG = "demo"; 
  private Button button_A; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    button_A = (Button)this.findViewById(R.id.Button_A); 
    button_A.setOnClickListener(new myButtonListener()); 
  } 
  private class myButtonListener implements OnClickListener{ 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(); 
      intent.setClass(ActivityDemoActivity.this, Activity01.class); 
      ActivityDemoActivity.this.startActivity(intent); 
      //感兴趣的朋友可以取消下面的代码注释,来测试finish方法的使用,会发现第一个activity会被强制终止 
      //ActivityDemoActivity.this.finish(); 
    } 
  }; 
  protected void onStart(){ 
    super.onStart(); 
    Log.i(TAG, "The activityDemo state---->onStart"); 
  } 
  protected void onRestart(){ 
    super.onRestart(); 
    Log.i(TAG, "The activityDemo state---->onReatart"); 
  } 
  protected void onResume(){ 
    super.onResume(); 
    Log.i(TAG, "The activityDemo state---->onResume"); 
  } 
  protected void onPause(){ 
    super.onPause(); 
    //调用isFinishing方法,判断activity是否要销毁 
    Log.i(TAG, "The activityDemo state---->onPause"); 
    if(isFinishing()){ 
      Log.w(TAG, "The activityDemo will be destroyed!"); 
    }else{ 
      Log.w(TAG, "The activityDemo is just pausing!"); 
    } 
  } 
  protected void onStop(){ 
    super.onStop(); 
    Log.i(TAG, "The activityDemo state---->onStop"); 
  } 
  protected void onDestroy(){ 
    super.onDestroy(); 
    Log.i(TAG, "The activityDemo state---->onDestroy"); 
  } 
}

Activity01.java


 
package uni.activity; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
public class Activity01 extends Activity{ 
  private static final String TAG = "demo"; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity01); 
    Log.d(TAG, "The activity01 state---->onStart"); 
  } 
   protected void onStart(){ 
      super.onStart(); 
      Log.d(TAG, "The activity01 state---->onStart"); 
    } 
    protected void onRestart(){ 
      super.onRestart(); 
      Log.d(TAG, "The activity01 state---->onReatart"); 
    } 
    protected void onResume(){ 
      super.onResume(); 
      Log.d(TAG, "The activity01 state---->onResume"); 
    } 
    protected void onPause(){ 
      super.onPause(); 
      Log.d(TAG, "The activity01 state---->onPause"); 
      //调用isFinishing方法,判断activity是否要销毁 
      if(isFinishing()){ 
        Log.w(TAG, "The activity01 will be destroyed!"); 
      }else{ 
        Log.w(TAG, "The activity01 is just pausing!"); 
      } 
    } 
    protected void onStop(){ 
      super.onStop(); 
      Log.d(TAG, "The activity01 state---->onStop"); 
    } 
    protected void onDestroy(){ 
      super.onDestroy(); 
      Log.d(TAG, "The activity01 state---->onDestroy"); 
    } 
} 

下面是演示的结果,

操作过程是:启动ActivityDemoActivity

然后单击按钮进入Activity01

(可见activity先暂停并且不会被释放,实际是个新activity压栈过程,然后新的activity开始,应该是onCreate然后onStart,我打印语句写错了,细心朋友应该看到了,当旧的activity不可见时,调用其onStop方法)

再按返回键回到ActivityDemoActivity

(返回后,处于栈顶的activity01会执行弹栈操作,显示将会被destroy)

再按返回键 回到桌面

其实并不复杂的东邪写的有些长了,但是基本上的显示了activity的完整的生命周期。

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android开发中Activity的生命周期及加载模式详解Android基础之Activity生命周期Android开发系列二之窗口Activity的生命周期Android Activity 横竖屏切换的生命周期Android中Fragment与Activity的生命周期对比深入解读Android开发中Activity的生命周期Android编程之基于Log演示一个activity生命周期实例详解Android中的Activity生命周期总结Android中Activity的生命周期探讨两分钟让你彻底明白Android Activity生命周期的详解(图文介绍)android横竖屏切换时候Activity的生命周期Android开发之activity的生命周期详解


--结束END--

本文标题: Android编程中activity的完整生命周期实例详解

本文链接: https://www.lsjlt.com/news/26171.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android开发Activity的生命周期详解
    目录前言典型情况下的生命周期分析前言 Android生命周期分为两部分:  (1)典型情况下的生命周期。(2)异常情况下的生命周期。 典型情况下的生命周期分析 图1 Ac...
    99+
    2024-04-02
  • Android Activity生命周期调用的理解
    目录状态启动模式操作APP时生命周期调用Activity异常生命周期总结状态 活动存放在一个叫返回栈的一个集合,当重新打开一个Activity时,它就会出现在栈顶。当要销毁该活动时...
    99+
    2024-04-02
  • Android入门教程之组件Activity的生命周期详解
    目录返回栈Activity 状态1. 运行状态2. 暂停状态3. 停止状态4. 销毁状态Activity 的生存期onCreate()onStart()onResume()onPau...
    99+
    2024-04-02
  • Android中Activity生命周期调用的示例分析
    这篇文章将为大家详细讲解有关Android中Activity生命周期调用的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。状态活动存放在一个叫返回栈的一个集合,当重新打开一个Activity时,它就...
    99+
    2023-06-22
  • Android  Activity生命周期和堆栈管理的详解
    Activity的生命周期Activity是Android中的四大组件之一,也是最基本,最重要的组件,是android系统提供一个可视化的,能与用户交换的组件。 系统提供的组件,不需要用户实例化,用户也不能实例化,是系统进行回调,例如web...
    99+
    2023-05-30
    android activity 生命周期
  • Activity跳转时生命周期跟踪的实例
    android应用当中Activity间相互跳转比较常见。本文将通过例子跟踪一下2个Activity间跳转过程中生命周期的变化过程。整个操作过程如下图所示: 打开应用,自动开启First Activity; 按下“Jump Button”按...
    99+
    2023-05-31
    activity 生命周期 ct
  • Android项目中如何使用Activity的生命周期
    Android项目中如何使用Activity的生命周期?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。Android Activity的生命周期详细介绍生命周期描述的是一个类...
    99+
    2023-05-31
    android activity roi
  • 详解ASP.NET MVC的整个生命周期
    目录一、介绍二、MVC生命周期详述View的初始化和渲染呈现三、结束一、介绍 我们做开发的,尤其是做微软技术栈的,有一个方向是跳不过去的,那就是MVC开发。我相信大家,做ASP.NE...
    99+
    2024-04-02
  • Android Activity的生命周期与启动模式全面解读
    目录Activity概述Activity生命周期生命周期的调用顺序演示Activity的启动模式启动模式的设置standard(默认standard)singleTopsingleT...
    99+
    2024-04-02
  • Android startService的使用与Service生命周期案例详解
    Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法,本文只探讨纯startServ...
    99+
    2024-04-02
  • Android bindService的使用与Service生命周期案例详解
    Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法,本文只探讨纯bindServi...
    99+
    2024-04-02
  • React中的生命周期详解
    目录react生命周期常用的生命周期不常用的生命周完整的生命周期图react生命周期 函数组件无生命周期,生命周期只有类组件才拥有 生命周期函数指在某一时刻组件会自动调用并执行的函数...
    99+
    2024-04-02
  • SpringBean生命周期之Bean的实例化详解
    目录前言实例化前阶段实例化阶段实例化后阶段总结前言 上一节说到了BeanDefinition的合并过程,这节该说Bean的实例化过程了。根据AbstractAutowireCapab...
    99+
    2024-04-02
  • Spring 中 Bean 的生命周期详解
    目录前言1.Bean 生命周期2.代码演示总结前言 Java 中的公共类称之为 Bean 或 Java Bean,而 Spring 中的 Bean 指的是将对象的生命周期,交个 Sp...
    99+
    2024-04-02
  • 详解kubernetes pod的编排和生命周期
    目录K8S Master基本架构Pod的编排思想Pod对象的属性和容器的属性?Pod的生命周期K8S Master基本架构       K8S的集群运行...
    99+
    2024-04-02
  • JavaWEB中Servlet的生命周期详解
    目录一、什么是Servlet 生命周期?二、Servlet 是由谁来维护的?三、Servlet 对象 是什么时候创建的?四、测试 Tomcat 什么时候被销毁?总结一、什么是Serv...
    99+
    2024-04-02
  • Java中Servlet的生命周期详解
    目录Web基础和HTTP协议什么是ServletServlet的生命周期Web基础和HTTP协议 ┌─────────┐ ┌──────...
    99+
    2024-04-02
  • Java线程的生命周期的详解
    Java线程的生命周期的详解对于多线程编程而言,理解线程的生命周期非常重要,本文就针对这一点进行讲解。一、线程的状态线程的存在有几种不同的状态,如下: New状态 Ready状态 Running状态 Dead状态 Non Runn...
    99+
    2023-05-30
    java 线程 生命周期
  • Angular中的生命周期实例分析
    今天小编给大家分享一下Angular中的生命周期实例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来...
    99+
    2024-04-02
  • Vue中的生命周期实例分析
    这篇文章主要介绍“Vue中的生命周期实例分析”,在日常操作中,相信很多人在Vue中的生命周期实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue中的生命周期实例分析”的疑惑有所帮助!接下来,请跟着小编...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作