ActivityStack 簡介,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的生命週期的變化圖
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中還有很多方法
這些都是用來調度Activity的,後面在啟動Activity中會涉及到這些方法的調用。