ActivityManagerService-B Activity狀態(tài)改變

版權(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
ActivityState.jpg
------> 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)隆箩;


ApplicationThread-schedule.png
------> 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)行交互了串稀;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市爹凹,隨后出現(xiàn)的幾起案子厨诸,更是在濱河造成了極大的恐慌,老刑警劉巖禾酱,帶你破解...
    沈念sama閱讀 210,914評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異绘趋,居然都是意外死亡颤陶,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,935評(píng)論 2 383
  • 文/潘曉璐 我一進(jìn)店門(mén)陷遮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)滓走,“玉大人,你說(shuō)我怎么就攤上這事帽馋〗练剑” “怎么了比吭?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,531評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)姨涡。 經(jīng)常有香客問(wèn)我衩藤,道長(zhǎng),這世上最難降的妖魔是什么涛漂? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,309評(píng)論 1 282
  • 正文 為了忘掉前任赏表,我火速辦了婚禮,結(jié)果婚禮上瓢剿,老公的妹妹穿的比我還像新娘。我一直安慰自己悠轩,他們只是感情好间狂,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,381評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布火架。 她就那樣靜靜地躺著,像睡著了一般距潘。 火紅的嫁衣襯著肌膚如雪炼列。 梳的紋絲不亂的頭發(fā)上音比,一...
    開(kāi)封第一講書(shū)人閱讀 49,730評(píng)論 1 289
  • 那天,我揣著相機(jī)與錄音洞翩,去河邊找鬼稽犁。 笑死骚亿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的来屠。 我是一名探鬼主播虑椎,決...
    沈念sama閱讀 38,882評(píng)論 3 404
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼俱笛!你這毒婦竟也來(lái)了捆姜?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,643評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤迎膜,失蹤者是張志新(化名)和其女友劉穎泥技,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體磕仅,經(jīng)...
    沈念sama閱讀 44,095評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡珊豹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,448評(píng)論 2 325
  • 正文 我和宋清朗相戀三年簸呈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片店茶。...
    茶點(diǎn)故事閱讀 38,566評(píng)論 1 339
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蜕便,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出忽妒,到底是詐尸還是另有隱情玩裙,我是刑警寧澤,帶...
    沈念sama閱讀 34,253評(píng)論 4 328
  • 正文 年R本政府宣布段直,位于F島的核電站吃溅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鸯檬。R本人自食惡果不足惜决侈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,829評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喧务。 院中可真熱鬧赖歌,春花似錦、人聲如沸功茴。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,715評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)坎穿。三九已至展父,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間玲昧,已是汗流浹背栖茉。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,945評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留孵延,地道東北人吕漂。 一個(gè)月前我還...
    沈念sama閱讀 46,248評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像尘应,于是被迫代替她去往敵國(guó)和親惶凝。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,440評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容