ActivityStack 简介

转载请标明出处: 

http://blog.csdn.net/yujun411522/article/details/46659413
本文出自:【yujun411522的博客】




在ActivityManagerService 的启动过程中出现了ActivityStack的这么一个类,这个类的作用是什么呢?

我们知道在Android中由Activity来负责展示和交互,而假如现在有这样的一个启动顺序Activity1->Activity2->Activity3,那么现在显示的是Activity3,我们在按住返回键的时候时回到Activity2,再按一次返回键,返回到Activity1。这种交互逻辑符合我们使用的习惯,这种后进先出的数据结构就是栈,android中就是使用stack来管理Activity的。
系统总是显示处于栈顶的Activity,每当启动一个新的Activity时就会入栈,原来处于栈顶的Activity被新的Activity取代,这时系统就会显示新的Activity。同样用户返回时,处于栈顶的Activity就会出栈,原栈顶后面的Activity处于栈顶位置,系统就会显示它。一直到该ActivityStack中所有的Activity都不再存在,这时返回到Home界面。

下面看看AndroidStack中有哪些重要的成员变量和方法
1 ActivityState
一个Activity可能处于的状态:    
    enum ActivityState {
        INITIALIZING,//正在初始化
        RESUMED,//获得焦点
        PAUSING,//正在暂停                                         
        PAUSED,//已经暂停
        STOPPING,//正在停止
        STOPPED,//已经停止
        FINISHING,//正在完成
        DESTROYING,//正在摧毁
        DESTROYED//已经摧毁
    }
状态的变化可以参看Activity的生命周期的变化图
ActivityStack 简介_第1张图片



2 ArrayList<ActivityRecord>
保存一系列的处于不同功能的Activity集合,这里使用的是ActivityRecord,这个类它用来记录每个Activity的运行信息,主要有以下几个Arraylist      
 /**
     * The back history of all previous (and possibly still running) activities.  It contains HistoryRecord objects.
     */
     //所有被返回的Activity
    final ArrayList<ActivityRecord> mHistory = new ArrayList<ActivityRecord>();
    
    /**
     * List of running activities, sorted by recent usage. The first entry in the list is the least recently used.
     * It contains HistoryRecord objects.
     */
     // 正在运行的Activity,按照LRU来排序,第一个是最近最少使用的
    final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();

    /**
     * List of activities that are waiting for a new activity to become visible before completing whatever operation they are
     * supposed to do.
     */
     //正在等待一个新的Activity变成Visible时的Activity集合
    final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();

    /**
     * List of activities that are ready to be stopped, but waiting for the next activity to settle down before doing so.  It contains
     * HistoryRecord objects.
     */
     //正准备被Stop,但是在等待另一个Activity完全stop时的Activity集合
    final ArrayList<ActivityRecord> mStoppingActivities  = new ArrayList<ActivityRecord>();

    /**
     * List of activities that are in the process of going to sleep.
     */
     // app进程中将要处于sleep的Activity集合
    final ArrayList<ActivityRecord> mGoingToSleepActivities= new ArrayList<ActivityRecord>();

    /**
     * Animations that for the current transition have requested not to be considered for the transition animation.
     */
     // 需不要迁移动画的Activity集合
    final ArrayList<ActivityRecord> mNoAnimActivities  = new ArrayList<ActivityRecord>();

    /**
     * List of activities that are ready to be finished, but waiting for the previous activity to settle down before doing so.  It contains
     * HistoryRecord objects.
     */
     // 正要别finished的,但是等待上一个Activityfinished完毕的Activity集合
    final ArrayList<ActivityRecord> mFinishingActivities   = new ArrayList<ActivityRecord>();


3 特殊的Activity
还有一些记录特殊状态的Activity,包括:
 /**
     * When we are in the process of pausing an activity, before starting the next one, this variable holds the activity that is currently being paused.
     */
     //当要暂定一个Activity时且没有启动一个新的Activity之前,用这个变量来保存正在处于暂停的Activity
    ActivityRecord mPausingActivity = null;

    /**
     * This is the last activity that we put into the paused state.  This is used to determine if we need to do an activity transition while sleeping,
     * when we normally hold the top activity paused.
     */
     //最近一个被暂停的Activity
    ActivityRecord mLastPausedActivity = null;

    /**
     * Current activity that is resumed, or null if there is none.
     */
    //正在处于resume状态的Activity
    ActivityRecord mResumedActivity = null;
    
    /**
     * This is the last activity that has been started.  It is only used to identify when multiple activities are started at once so that the user
     * can be warned they may not be in the activity they think they are.
     */
     //最近被启动的Activity。
    ActivityRecord mLastStartedActivity = null;
这些变量就是用来记录和管理Activity的数据结构,ActivityStack中还有很多方法
ActivityStack 简介_第2张图片
这些都是用来调度Activity的,后面在启动Activity中会涉及到这些方法的调用。

你可能感兴趣的:(ActivityStack 简介)