版權(quán)說(shuō)明:本文為 開(kāi)開(kāi)向前沖 原創(chuàng)文章咽扇,轉(zhuǎn)載請(qǐng)注明出處;
注:限于作者水平有限吼蚁,文中有不對(duì)的地方還請(qǐng)指教
很多大神都講解過(guò)AMS是如何管理Activity讶凉,我也通過(guò)看他們的博客學(xué)習(xí)了很多,大多數(shù)文章通過(guò)講解Activity 的啟動(dòng)過(guò)程來(lái)說(shuō)明這個(gè)過(guò)程瘫辩,本文根據(jù)以ActivityState的切換為主線來(lái)說(shuō)明AMS 是如何管理Activity伏嗜;
ActivityState
------> frameworks\base\services\core\java\com\android\server\am\ActivityStack.java
enum ActivityState {
INITIALIZING,
RESUMED,
PAUSING,
PAUSED,
STOPPING,
STOPPED,
FINISHING,
DESTROYING,
DESTROYED
}
Activity在生命周期內(nèi)肯定處于上述狀態(tài)中的一種坛悉;Activity 狀態(tài)的切換也是AMS管理Activity的一個(gè)體現(xiàn);
1. 初始狀態(tài) INITIALIZING
Activity 最初始的狀態(tài)就是 INITIALIZING承绸,INITIALIZING 字面值代表初始化裸影,那 ActivityRecord 會(huì)在什么時(shí)候被初始化呢?初始化一般發(fā)生在構(gòu)造這個(gè)對(duì)象的時(shí)候军熏,進(jìn)入ActivityRecord 的構(gòu)造方法轩猩,有如下:
ActivityRecord(......) {
......
state = ActivityState.INITIALIZING; //初始化狀態(tài)
......
}
ActivityRecord 的state對(duì)象代表著ActivityRecord的各種狀態(tài);現(xiàn)在的問(wèn)題變成ActivityRecord在什么時(shí)候會(huì)被初始化呢荡澎?看過(guò)startActivity流程源碼的應(yīng)該都知道ActivityRecord會(huì)在ActivityStackSupervisor的startActivityLocked方法中被初始化界轩;
------> ActivityStackSupervisor.java
final int startActivityLocked(IApplicationThread caller,
Intent intent, String resolvedType, ActivityInfo aInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode,
int callingPid, int callingUid, String callingPackage,
int realCallingPid, int realCallingUid, int startFlags, Bundle options,
boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
TaskRecord inTask) {
......
ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
requestCode, componentSpecified, this, container, options);
......
}
跟蹤startActivityLocked的調(diào)用棧就知道它的源頭是在ContextImpl.java的startActivity,即平時(shí)啟動(dòng)Activity時(shí)調(diào)用的startActivity()方法衔瓮;即 ActivityRecord會(huì)在startActivity過(guò)程中被初始化浊猾;這很符合邏輯,不管以什么方式打開(kāi)應(yīng)用Activity热鞍,都會(huì)走到 startActivityLocked葫慎,這里會(huì)初始化一個(gè)ActivityRecord 對(duì)象代表新啟動(dòng)的Activity,AMS 通過(guò)管理這個(gè)ActivityRecord對(duì)象來(lái)管理Activity的生命周期薇宠;
2. 持續(xù)狀態(tài) RESUMED
RESUMED 狀態(tài)代表該Activity 處于可見(jiàn)狀態(tài)偷办,說(shuō)明AMS已經(jīng)調(diào)整好ActivityRecord的狀態(tài),可以向用戶顯示澄港;RESUMED 是在ActivityStack.java的resumeTopActivityInnerLocked方法中設(shè)置:
------> ActivityStack.java
final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
......
if (next.app != null && next.app.thread != null) {
......
ActivityState lastState = next.state;
mService.updateCpuStats();
if (DEBUG_STATES) Slog.v(TAG, "Moving to RESUMED: " + next + " (in existing)");
next.state = ActivityState.RESUMED;//設(shè)置狀態(tài)為 RESUMED
mResumedActivity = next;
next.task.touchActiveTime();
mService.addRecentTaskLocked(next.task);
mService.updateLruProcessLocked(next.app, true, null);
updateLRUListLocked(next);
mService.updateOomAdjLocked();
......
}
......
}
resumeTopActivityInnerLocked 方法會(huì)在startActivity過(guò)程中被調(diào)用椒涯,該方法會(huì)根據(jù)需要啟動(dòng)的Activity所在進(jìn)程是否已經(jīng)啟動(dòng)執(zhí)行不同操作;
3. 暫停狀態(tài) PAUSING ---> PAUSED
Activity 從可交互狀態(tài)到不可交互狀態(tài)需要一個(gè)過(guò)渡期回梧,這個(gè)過(guò)渡期就是PAUSING 狀態(tài)废岂,PAUSING 后Activity會(huì)進(jìn)入PAUSED狀態(tài),代表該Activity 處于正在暫停的過(guò)程狱意,說(shuō)明AMS 正在調(diào)整Activity 狀態(tài)湖苞,使其不再和用戶交互;在啟動(dòng)新Activity的時(shí)候详囤,會(huì)先暫停目前處于RESUMED狀態(tài)的Activity财骨,此時(shí)會(huì)回調(diào)兩個(gè)應(yīng)用的Lifecycle方法,Lifecycle 方法是AMS 通過(guò)回調(diào)ActivityThread中ApplicationThread的方法來(lái)調(diào)用(此時(shí)ActivityThread為Server藏姐,AMS為Client)隆箩;
------> ActivityStack.java
/**
* Start pausing the currently resumed activity. It is an error to call this if there
* is already an activity being paused or there is no resumed activity.
*
* @param userLeaving True if this should result in an onUserLeaving to the current activity.
* @param uiSleeping True if this is happening with the user interface going to sleep (the
* screen turning off).
* @param resuming True if this is being called as part of resuming the top activity, so
* we shouldn't try to instigate a resume here.
* @param dontWait True if the caller does not want to wait for the pause to complete. If
* set to true, we will immediately complete the pause here before returning.
* @return Returns true if an activity now is in the PAUSING state, and we are waiting for
* it to tell us when it is done.
*/
final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
boolean dontWait) {
ActivityRecord prev = mResumedActivity;//將正處于RESUMED狀態(tài)的Activity標(biāo)記為prev(A->B,A的從RESUMED->PAUSING)
mResumedActivity = null;
mPausingActivity = prev;//啟動(dòng)新Activity的Activity將處于mPausingActivity狀態(tài)
mLastPausedActivity = prev;
prev.state = ActivityState.PAUSING;//設(shè)置狀態(tài)為PAUSING
prev.task.touchActiveTime();
if (prev.app != null && prev.app.thread != null) {
if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
try {
prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
userLeaving, prev.configChangeFlags, dontWait);//調(diào)用Activity LifeCycle的onPause方法
}
} else {
mPausingActivity = null;
mLastPausedActivity = null;
mLastNoHistoryActivity = null;
}
if (mPausingActivity != null) {
if (dontWait) {
// If the caller said they don't want to wait for the pause, then complete
// the pause now.
completePauseLocked(false);//設(shè)置Activity狀態(tài)為PAUSED
return false;
} else {
// Schedule a pause timeout in case the app doesn't respond.
// We don't give it much time because this directly impacts the
// responsiveness seen by the user.
Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
msg.obj = prev;
prev.pauseTime = SystemClock.uptimeMillis();
mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
return true;
}
} else {
// This activity failed to schedule the
// pause, so just treat it as being paused now.
if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
if (!resuming) {
mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);//會(huì)重新進(jìn)入設(shè)置狀態(tài)為PAUSED
}
return false;
}
}
private void completePauseLocked(boolean resumeNext) {
ActivityRecord prev = mPausingActivity;
if (prev != null) {
prev.state = ActivityState.PAUSED; //設(shè)置前一個(gè)Activity的狀態(tài)為 PAUSED
......
}
......
// Notfiy when the task stack has changed
mService.notifyTaskStackChangedLocked();
}
4. 停止?fàn)顟B(tài) STOPPING ---> STOPPED
STOPPING ---> STOPPED 代表Activity停止的過(guò)程和狀態(tài);對(duì)應(yīng)過(guò)程Activity LifeCycle的onStop() 方法會(huì)被回調(diào)羔杨;
final void stopActivityLocked(ActivityRecord r) {
......
if (r.app != null && r.app.thread != null) {
adjustFocusedActivityLocked(r, "stopActivity");
r.resumeKeyDispatchingLocked();
try {
r.stopped = false;
if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
+ " (stop requested)");
r.state = ActivityState.STOPPING;//設(shè)置前一個(gè)Activity 的狀態(tài)為STOPPING
Log.d(TAG + "STOPPING", Log.getStackTraceString(new Throwable()));//keiven-chen
if (!r.visible) {
mWindowManager.setAppVisibility(r.appToken, false);
}
r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);//回調(diào)Activity LifeCycle的onStop()方法
if (mService.isSleepingOrShuttingDown()) {
r.setSleeping(true);
}
Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
} catch (Exception e) {
// Maybe just ignore exceptions here... if the process
// has crashed, our death notification will clean things
// up.
Slog.w(TAG, "Exception thrown during pause", e);
// Just in case, assume it to be stopped.
r.stopped = true;
r.state = ActivityState.STOPPED;
Log.d(TAG+"STOPPED",Log.getStackTraceString(new Throwable()));//Keiven-chen
}
}
}
final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
PersistableBundle persistentState, CharSequence description) { //通過(guò)該方法設(shè)置Activity狀態(tài)為 STOPPED
......
if (icicle != null) {
// If icicle is null, this is happening due to a timeout, so we
// haven't really saved the state.
r.icicle = icicle;
r.haveState = true;
r.launchCount = 0;
r.updateThumbnailLocked(null, description);
}
if (!r.stopped) {
if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r + " (stop complete)");
mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
r.stopped = true;
r.state = ActivityState.STOPPED; //設(shè)置前一個(gè)Activity 的狀態(tài)為STOPPED
Log.d(TAG+"STOPPED",Log.getStackTraceString(new Throwable()));//Keiven-chen
if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
mStackSupervisor.requestVisibleBehindLocked(r, false);
}
if (r.finishing) {
r.clearOptionsLocked();
} else {
if (r.configDestroy) {
destroyActivityLocked(r, true, "stop-config");
mStackSupervisor.resumeTopActivitiesLocked();
} else {
mStackSupervisor.updatePreviousProcessLocked(r);
}
}
}
}
5. FINISHING, DESTROYING, DESTROYED
Activity 會(huì)占用很多系統(tǒng)資源捌臊,當(dāng)Activity 不需要顯示的時(shí)候需要回收相關(guān)資源,這個(gè)過(guò)程涉及到的三個(gè)狀態(tài)就是 FINISHING, DESTROYING, DESTROYED问畅;
FINISHING 狀態(tài)在 finishCurrentActivityLocked方法中設(shè)置娃属;
final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
......
r.state = ActivityState.STOPPING;//設(shè)置Activity 的狀態(tài)為STOPPING
Log.d(TAG + "STOPPING",Log.getStackTraceString(new Throwable()));//Keiven-chen
if (oomAdj) {
mService.updateOomAdjLocked();
}
return r;
}
// make sure the record is cleaned out of other places.
mStackSupervisor.mStoppingActivities.remove(r);
mStackSupervisor.mGoingToSleepActivities.remove(r);
mStackSupervisor.mWaitingVisibleActivities.remove(r);
if (mResumedActivity == r) {
mResumedActivity = null;
}
final ActivityState prevState = r.state;
r.state = ActivityState.FINISHING; //設(shè)置Activity 狀態(tài)為FINISHING
Log.d(TAG + "FINISHING",Log.getStackTraceString(new Throwable()));//Keiven-chen
if (mode == FINISH_IMMEDIATELY
|| prevState == ActivityState.STOPPED
|| prevState == ActivityState.INITIALIZING) {
// If this activity is already stopped, we can just finish
// it right now.
r.makeFinishing();
boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");//該方法會(huì)銷(xiāo)毀Activity六荒,即Activity通過(guò)該方法進(jìn)入 DESTROYING狀態(tài);
if (activityRemoved) {
mStackSupervisor.resumeTopActivitiesLocked();
}
return activityRemoved ? null : r;
}
mStackSupervisor.mFinishingActivities.add(r);
r.resumeKeyDispatchingLocked();
mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
return r;
}
Recents中清除應(yīng)用會(huì)調(diào)用ProcessRecord的Kil掉進(jìn)程矾端,Kill進(jìn)程就會(huì)導(dǎo)致進(jìn)程的Binder死亡掏击,會(huì)觸發(fā)binderDied被調(diào)用,該方法會(huì)回收每一個(gè)Activity 資源秩铆,這樣Activity就處于DESTROYED狀態(tài)了砚亭,即Activity 處于完全銷(xiāo)毀狀態(tài);
/**
* Perform the common clean-up of an activity record. This is called both
* as part of destroyActivityLocked() (when destroying the client-side
* representation) and cleaning things up as a result of its hosting
* processing going away, in which case there is no remaining client-side
* state to destroy so only the cleanup here is needed.
*/
final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
boolean setState) {
if (mResumedActivity == r) {
mResumedActivity = null;
}
if (mPausingActivity == r) {
mPausingActivity = null;
}
mService.clearFocusedActivity(r);
r.configDestroy = false;
r.frozenBeforeDestroy = false;
if (setState) {
r.state = ActivityState.DESTROYED; //設(shè)置Activity的狀態(tài)為DESTROYED
Log.d(TAG + "DESTROYED",Log.getStackTraceString(new Throwable()));//Keiven-chen
r.app = null;
}
// Make sure this record is no longer in the pending finishes list.
// This could happen, for example, if we are trimming activities
// down to the max limit while they are still waiting to finish.
mStackSupervisor.mFinishingActivities.remove(r);
mStackSupervisor.mWaitingVisibleActivities.remove(r);
// Remove any pending results.
if (r.finishing && r.pendingResults != null) {
for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
PendingIntentRecord rec = apr.get();
if (rec != null) {
mService.cancelIntentSenderLocked(rec, false);
}
}
r.pendingResults = null;
}
if (cleanServices) {
cleanUpActivityServicesLocked(r);
}
// Get rid of any pending idle timeouts.
removeTimeoutsForActivityLocked(r);
if (getVisibleBehindActivity() == r) {
mStackSupervisor.requestVisibleBehindLocked(r, false);
}
}
Log演示:下面是Activity-A 通過(guò)startActivity方法打開(kāi)Activity-B過(guò)程中兩個(gè)Activity 狀態(tài)變化過(guò)程LOG
11-13 15:54:18.964 2899-3804/system_process I/ActivityManager: START u0 {cmp=test.activitytest/.SecondActivity} from uid 10061 on display 0
11-13 15:54:18.964 2899-3804/system_process D/ActivityManagerINITIALIZING: java.lang.Throwable //Activity-B 處于INITIALIZING狀態(tài)
at com.android.server.am.ActivityRecord.<init>(ActivityRecord.java:405)
at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1527)
at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:995)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3497)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3484)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:140)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 15:54:18.973 2899-3804/system_process D/ActivityManagerPAUSING: java.lang.Throwable //Activity-A 處于 PAUSING狀態(tài)
at com.android.server.am.ActivityStack.startPausingLocked(ActivityStack.java:827)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1642)
at com.android.server.am.ActivityStack.resumeTopActivityLocked(ActivityStack.java:1480)
at com.android.server.am.ActivityStackSupervisor.resumeTopActivitiesLocked(ActivityStackSupervisor.java:2518)
at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:2127)
at com.android.server.am.ActivityStackSupervisor.startActivityUncheckedLocked(ActivityStackSupervisor.java:2258)
at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1560)
at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:995)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3497)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3484)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:140)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 15:54:18.992 2899-4338/system_process D/ActivityManagerPAUSED: java.lang.Throwable //Activity-A 處于 PAUSED狀態(tài)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:974)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:916)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:6459)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:513)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 15:54:19.003 2899-4338/system_process D/ActivityManagerRESUMED: java.lang.Throwable // Activity-B 處于RESUMED狀態(tài)
at com.android.server.am.ActivityStack.minimalResumeActivityLocked(ActivityStack.java:659)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1279)
at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1326)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1928)
at com.android.server.am.ActivityStack.resumeTopActivityLocked(ActivityStack.java:1480)
at com.android.server.am.ActivityStackSupervisor.resumeTopActivitiesLocked(ActivityStackSupervisor.java:2518)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1020)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:916)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:6459)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:513)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 15:54:19.157 2899-2942/system_process I/ActivityManager: Displayed test.activitytest/.SecondActivity: +165ms
11-13 15:54:19.186 2899-2937/system_process D/ActivityManagerSTOPPING: java.lang.Throwable //Activity-A 處于STOPPING狀態(tài)
at com.android.server.am.ActivityStack.stopActivityLocked(ActivityStack.java:2548)
at com.android.server.am.ActivityStackSupervisor.activityIdleInternalLocked(ActivityStackSupervisor.java:2391)
at com.android.server.am.ActivityStackSupervisor$ActivityStackSupervisorHandler.activityIdleInternal(ActivityStackSupervisor.java:3586)
at com.android.server.am.ActivityStackSupervisor$ActivityStackSupervisorHandler.handleMessage(ActivityStackSupervisor.java:3608)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
11-13 15:54:19.209 2899-4338/system_process D/ActivityManagerSTOPPED: java.lang.Throwable //Activity-A 處于STOPPED狀態(tài)
at com.android.server.am.ActivityStack.activityStoppedLocked(ActivityStack.java:951)
at com.android.server.am.ActivityManagerService.activityStopped(ActivityManagerService.java:6480)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:524)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
上面是從Activity-A跳轉(zhuǎn)到Activity-B的狀態(tài)變化調(diào)用棧殴玛,此時(shí)在Activity-B的界面點(diǎn)擊Recents捅膘,然后清除當(dāng)前任務(wù)棧,此時(shí)Activity-A跳轉(zhuǎn)到Activity-B的狀態(tài)變化調(diào)用棧如下:
11-13 16:13:44.658 2899-4434/system_process D/ActivityManagerFINISHING: java.lang.Throwable
at com.android.server.am.ActivityStack.finishCurrentActivityLocked(ActivityStack.java:2825)
at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2775)
at com.android.server.am.TaskRecord.performClearTaskAtIndexLocked(TaskRecord.java:613)
at com.android.server.am.TaskRecord.removeTaskActivitiesLocked(TaskRecord.java:702)
com.android.server.am.ActivityManagerService.removeTaskByIdLocked(ActivityManagerService.java:8627)
at com.android.server.am.ActivityManagerService.removeTask(ActivityManagerService.java:8645)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:1908)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 16:13:44.659 2899-4434/system_process D/ActivityManagerDESTROYING: java.lang.Throwable
at com.android.server.am.ActivityStack.destroyActivityLocked(ActivityStack.java:3265)
at com.android.server.am.ActivityStack.finishCurrentActivityLocked(ActivityStack.java:2833)
at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2775)
at com.android.server.am.TaskRecord.performClearTaskAtIndexLocked(TaskRecord.java:613)
at com.android.server.am.TaskRecord.removeTaskActivitiesLocked(TaskRecord.java:702)
at com.android.server.am.ActivityManagerService.removeTaskByIdLocked(ActivityManagerService.java:8627)
at com.android.server.am.ActivityManagerService.removeTask(ActivityManagerService.java:8645)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:1908)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 16:13:44.661 2899-4434/system_process D/ActivityManagerFINISHING: java.lang.Throwable
at com.android.server.am.ActivityStack.finishCurrentActivityLocked(ActivityStack.java:2825)
at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2775)
at com.android.server.am.TaskRecord.performClearTaskAtIndexLocked(TaskRecord.java:613)
at com.android.server.am.TaskRecord.removeTaskActivitiesLocked(TaskRecord.java:702)
at com.android.server.am.ActivityManagerService.removeTaskByIdLocked(ActivityManagerService.java:8627)
at com.android.server.am.ActivityManagerService.removeTask(ActivityManagerService.java:8645)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:1908)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 16:13:44.879 2899-4434/system_process D/ActivityManagerDESTROYING: java.lang.Throwable
at com.android.server.am.ActivityStack.destroyActivityLocked(ActivityStack.java:3265)
at com.android.server.am.ActivityStack.finishCurrentActivityLocked(ActivityStack.java:2833)
at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2775)
at com.android.server.am.TaskRecord.performClearTaskAtIndexLocked(TaskRecord.java:613)
at com.android.server.am.TaskRecord.removeTaskActivitiesLocked(TaskRecord.java:702)
at com.android.server.am.ActivityManagerService.removeTaskByIdLocked(ActivityManagerService.java:8627)
at com.android.server.am.ActivityManagerService.removeTask(ActivityManagerService.java:8645)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:1908)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 16:13:44.879 2899-4434/system_process D/ActivityManagerKill: java.lang.Throwable//Kill 進(jìn)程滚粟,釋放資源
at com.android.server.am.ProcessRecord.kill(ProcessRecord.java:530)
at com.android.server.am.ActivityManagerService.cleanUpRemovedTaskLocked(ActivityManagerService.java:8569)
at com.android.server.am.ActivityManagerService.removeTaskByIdLocked(ActivityManagerService.java:8628)
at com.android.server.am.ActivityManagerService.removeTask(ActivityManagerService.java:8645)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:1908)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2253)
at android.os.Binder.execTransact(Binder.java:446)
11-13 16:13:44.997 2899-4436/system_process D/ActivityManagerDESTROYED: java.lang.Throwable
at com.android.server.am.ActivityStack.removeActivityFromHistoryLocked(ActivityStack.java:3066)
at com.android.server.am.ActivityStack.removeHistoryRecordsForAppLocked(ActivityStack.java:3446)
at com.android.server.am.ActivityStack.handleAppDiedLocked(ActivityStack.java:3994)
at com.android.server.am.ActivityStackSupervisor.handleAppDiedLocked(ActivityStackSupervisor.java:2435)
at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:4698)
at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:4850)
at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1228)
at android.os.BinderProxy.sendDeathNotice(Binder.java:551)
11-13 16:14:00.198 2899-4436/system_process D/ActivityManagerDESTROYED: java.lang.Throwable
at com.android.server.am.ActivityStack.cleanUpActivityLocked(ActivityStack.java:3012)
at com.android.server.am.ActivityStack.removeHistoryRecordsForAppLocked(ActivityStack.java:3467)
at com.android.server.am.ActivityStack.handleAppDiedLocked(ActivityStack.java:3994)
at com.android.server.am.ActivityStackSupervisor.handleAppDiedLocked(ActivityStackSupervisor.java:2435)
at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:4698)
at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:4850)
at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1228)
at android.os.BinderProxy.sendDeathNotice(Binder.java:551)
11-13 16:14:00.356 2899-4436/system_process D/ActivityManagerDESTROYED: java.lang.Throwable
at com.android.server.am.ActivityStack.removeActivityFromHistoryLocked(ActivityStack.java:3066)
at com.android.server.am.ActivityStack.removeHistoryRecordsForAppLocked(ActivityStack.java:3446)
at com.android.server.am.ActivityStack.handleAppDiedLocked(ActivityStack.java:3994)
at com.android.server.am.ActivityStackSupervisor.handleAppDiedLocked(ActivityStackSupervisor.java:2435)
at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:4698)
at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:4850)
at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1228)
at android.os.BinderProxy.sendDeathNotice(Binder.java:551)
11-13 16:14:00.560 2899-4436/system_process D/ActivityManagerDESTROYED: java.lang.Throwable
at com.android.server.am.ActivityStack.cleanUpActivityLocked(ActivityStack.java:3012)
at com.android.server.am.ActivityStack.removeHistoryRecordsForAppLocked(ActivityStack.java:3467)
at com.android.server.am.ActivityStack.handleAppDiedLocked(ActivityStack.java:3994)
at com.android.server.am.ActivityStackSupervisor.handleAppDiedLocked(ActivityStackSupervisor.java:2435)
at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:4698)
at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:4850)
at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1228)
at android.os.BinderProxy.sendDeathNotice(Binder.java:551)
由于我編寫(xiě)的APP 包含兩個(gè)Activity(Activity-A寻仗,Activity-B),所以在Recents中清除該APP時(shí)會(huì)將兩個(gè)兩個(gè)Activity資源回收凡壤,所以FINISHING署尤,DESTROYING,DESTROYED 狀態(tài)都是成雙出現(xiàn)亚侠;
總結(jié):至此Activity 的各種狀態(tài)都已經(jīng)說(shuō)完曹体,完整的狀態(tài)切換過(guò)程如下(Activity-A —> Activity-B)
RESUMED(A)->INITIALIZING(B)->PAUSING(A)->PAUSED(A)->RESUMED(B)->STOPING(A)->STOPED(A)->FINISHING(A) (正常從A->B 啟動(dòng)情況,這種情況不涉及到Destroy Activity)
RESUMED(A)->INITIALIZING(B)->PAUSING(A)->PAUSED(A)->RESUMED(B)->STOPING(A)->STOPED(A)->FINISHING(A)->DESTROYING(A)->DESTROYED(A)(涉及銷(xiāo)毀情況硝烂,這里只銷(xiāo)毀A箕别,B的銷(xiāo)毀過(guò)程類似),當(dāng)Activity 處于RESUMED狀態(tài)時(shí)滞谢,我們就可以和該Activity進(jìn)行交互了串稀;