应用内启动Activity

应用内启动Activity流程图

1 Activity会执行的startActivity有几种重载方法,但最终会调用startActivityForResult

2 Activity 的startActivityForResult会调用Instrumentation的execStartActivity方法

Instrumentation.ActivityResult ar =
                 mInstrumentation.execStartActivity(
                     this, mMainThread.getApplicationThread(), mToken, this,
                     intent, requestCode, options);

3 Instrumentation的execStartActivity方法会调用 ActivityManagerNative的startActivity

int result = ActivityManagerNative.getDefault()
                 .startActivity(whoThread, who.getBasePackageName(), intent,
                         intent.resolveTypeIfNeeded(who.getContentResolver()),
                         token, target != null ? target.mEmbeddedID : null,
                         requestCode, 0, null, options);

ps:这个方法最后还会执行checkStartActivityResult来检查是否启动成功。

4 ActivityManagerNative的gDefault会通过Binder返回ActivityManagerService的单例。实际上是调用的是ActivityManagerService的startActivity方法,会调用startActivityAsUser

startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
             resultWho, requestCode, startFlags, profilerInfo, options,
             UserHandle.getCallingUserId());
5 ActivityManagerService的` startActivityAsUser `会调用ActivityStackSupervisor的` startActivityMayWait `
mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
                 resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
                 profilerInfo, null, null, options, userId, null, null);

6 ActivityStackSupervisor的startActivityMayWait会调用startActivityLocked

int res = startActivityLocked(caller, intent, resolvedType, aInfo,
                     voiceSession, voiceInteractor, resultTo, resultWho,
                     requestCode, callingPid, callingUid, callingPackage,
                     realCallingPid, realCallingUid, startFlags, options,
                     componentSpecified, null, container, inTask);

7 ActivityStackSupervisor的startActivityLocked会调用startActivityUncheckedLocked

err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
                 startFlags, true, options, inTask);  

8 ActivityStackSupervisor的startActivityUncheckedLocked会调用ActivityStack的startActivityLocked

targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);  

9 ActivityStack的startActivityLocked会ActivityStackSupervisor调用的resumeTopActivitiesLocked

mStackSupervisor.resumeTopActivitiesLocked(this, r, options);

10 ActivityStackSupervisor的resumeTopActivitiesLocked又会调用回ActivityStack的resumeTopActivityLocked

 result = targetStack.resumeTopActivityLocked(target, targetOptions);

11 ActivityStack的resumeTopActivityLocked又会调用自身的resumeTopActivityInnerLocked

result = resumeTopActivityInnerLocked(prev, options);

12 ActivityStack的resumeTopActivityInnerLocked会调用自身的startPausingLocked先暂停之前的Activity

pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);

PS: 这里会结束掉这个resumeTopActivityInnerLocked,返回true

if (pausing) {
             if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG,
                     "resumeTopActivityLocked: Skip resume: need to start pausing");
             // At this point we want to put the upcoming activity's process
             // at the top of the LRU list, since we know we will be needing it
             // very soon and it would be a waste to let it get killed if it
             // happens to be sitting towards the end.
             if (next.app != null && next.app.thread != null) {
                 mService.updateLruProcessLocked(next.app, true, null);
             }
             if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
             return true;
         }  

13 ActivityStack的startPausingLocked会调用ActivityRecord的ProcessRecord
的IApplicationThread,通过Binder实际上调用ActivityThread的ApplicationThread的schedulePauseActivity

prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
                         userLeaving, prev.configChangeFlags, dontWait);

14 ActivityThread的ApplicationThread的schedulePauseActivity发送消息最后会调用ActivityThread的handlePauseActivity,最后会调用ActivityManagerService的activityPaused

PS:performPauseActivity会调用Activity的onPause

ActivityManagerNative.getDefault().activityPaused(token);

15 ActivityManagerService的activityPaused会调用ActivityStack的activityPausedLocked

 stack.activityPausedLocked(token, false);

16 用ActivityStack的activityPausedLocked会调用自身的completePauseLocked

completePauseLocked(true);

17 用ActivityStack的completePauseLocked又会调用ActivityStackSupervisor的resumeTopActivitiesLocked

mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);

18 ActivityStackSupervisor的resumeTopActivitiesLocked又会调用回ActivityStack的resumeTopActivityLocked

 result = targetStack.resumeTopActivityLocked(target, targetOptions);

19 ActivityStack的resumeTopActivityLocked又会调用自身的resumeTopActivityInnerLocked

result = resumeTopActivityInnerLocked(prev, options);

20 ActivityStack的resumeTopActivityInnerLocked会调用ActivityStackSupervisor的startSpecificActivityLocked

if (next.app != null && next.app.thread != null) {
 ...
}else{
mStackSupervisor.startSpecificActivityLocked(next, true, true);
}  

21 ActivityStackSupervisor的startSpecificActivityLocked,因为应用程序已经运行,会调用ActivityStackSupervisor的realStartActivityLocked

        // Is this activity's application already running?
        ProcessRecord app = mService.getProcessRecordLocked(r.processName,
                r.info.applicationInfo.uid, true);

        r.task.stack.setLaunchTime(r);
        if (app != null && app.thread != null) {
            try {
                if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
                        || !"android".equals(r.info.packageName)) {
                    // Don't add this if it is a platform component that is marked
                    // to run in multiple processes, because this is actually
                    // part of the framework so doesn't make sense to track as a
                    // separate apk in the process.
                    app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
                            mService.mProcessStats);
                }
                realStartActivityLocked(r, app, andResume, checkConfig);
                return;
            } catch (RemoteException e) {
                Slog.w(TAG, "Exception when starting activity "
                        + r.intent.getComponent().flattenToShortString(), e);
            }

            // If a dead object exception was thrown -- fall through to
            // restart the application.
        }

22 ActivityStackSupervisor的realStartActivityLocked会调用ActivityThread的ApplicationThread的scheduleLaunchActivity

            app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
                     System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
                     r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState,
                     r.icicle, r.persistentState, results, newIntents, !andResume,

23 ApplicationThread的scheduleLaunchActivity最终会调用ActivityThread的handleLaunchActivity

24 ActivityThread的handleLaunchActivity会调用自身的performLaunchActivity

Activity a = performLaunchActivity(r, customIntent);

25 ActivityThread的performLaunchActivity会调用Instrumentation的callActivityOnCreate

mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);

26 Instrumentation的callActivityOnCreate会调用Activity的performCreate

activity.performCreate(icicle);

27 Activity的performCreate会调用自身的onCreate,至此Activity就onCreateonCreate(icicle);

总结
  • 和Launcher启动应用程序区别在于应用已启动,在21步的时候,不用新建PID和运行ActivityThread,而是直接启动Activity。
参考
  • Android应用程序内部启动Activity过程(startActivity)的源代码分析

  • 任务和返回栈

你可能感兴趣的:(应用内启动Activity)