图解Activity启动过程

Android 8.0 Activity启动过程分析_第1张图片

上图大致分析出了Android-8.0应用根Activity的启动过程,其他Activity的启动也是类似的。

相关类说明

packages/apps/Launcher3/src/com/android/launcher3/- Launcher.javaframeworks/base/core/java/android/app/- Activity.java- Instrumentation.java- ActivityManager.java- IActivityManager.aidl- ActivityThread.java    - ApplicationThread.java    - H.java- IApplicationThread.aidlframeworks/base/services/core/java/com/android/server/am/- ActivityManagerService.java- ActivityStarter.java- ActivityStackSupervisor.java- ActivityStack.java- ActivityRecord.java- ProcessRecord.java
  • Instrumentation:字面意思“仪器”。官方解释:用于实现应用程序检测代码的基类。 当开启仪器运行时,该类将在任何应用程序代码之前为您实例化,从而允许您监视系统与应用程序之间的所有交互。Instrumentation实现通过AndroidManifest.xml向系统描述。
  • ActivityManager:此类提供有关活动,服务和包含过程的信息和交互。
  • IActivityManager:用于与ActivityManagerService交谈的系统专用API。 提供了从应用程序返回到活动管理器的调用。
  • ActivityThread:管理应用程序进程中主线程的执行,根据ActivityManager请求调度和执行Activitys、broadcasts和其他操作。
  • ApplicationThreadActivityThread内部类,IApplicationThread.aidl的具体实现,提供给ActivityManagerActivityManager通过它告知应用程序将要做的事。
  • H:继承HandlerActivityThread内部类,是应用程序进程中主线程的消息管理类。
  • ActivityManagerService:负责系统中四大组件的启动、切换、调度及应用进程的管理和调度等工作。
  • ActivityStarter:用于解释如何启动活动的控制器。此类收集用于确定如何将意图和标志转变为活动以及相关任务和堆栈的所有逻辑。
  • ActivityStack: 单个活动栈的状态和管理。
  • ActivityStackSupervisor:Activity栈管理。

具体过程分析

Launcher.startActivitySafely()

Launcher启动后会将已安装应用的图标显示到桌面,图标就是我们启动应用的入口。当我们点击图标时,会调用Launcher中的startActivitySafely

public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {   if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {       Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();       return false;   }   // Only launch using the new animation if the shortcut has not opted out (this is a   // private contract between launcher and may be ignored in the future).   boolean useLaunchAnimation = (v != null) &&           !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);   Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;   UserHandle user = item == null ? null : item.user;   // Prepare intent   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//注释1   if (v != null) {       intent.setSourceBounds(getViewBounds(v));   }   try {       if (Utilities.ATLEAST_MARSHMALLOW               && (item instanceof ShortcutInfo)               && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT                || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)               && !((ShortcutInfo) item).isPromise()) {           // Shortcuts need some special checks due to legacy reasons.           startShortcutIntentSafely(intent, optsBundle, item);       } else if (user == null || user.equals(Process.myUserHandle())) {           // Could be launching some bookkeeping activity           startActivity(intent, optsBundle);//注释2       } else {           LauncherAppsCompat.getInstance(this).startActivityForProfile(                   intent.getComponent(), user, intent.getSourceBounds(), optsBundle);       }       return true;   } catch (ActivityNotFoundException|SecurityException e) {       Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();       Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);   }   return false;}

上面就是startActivitySafely的代码,首先我们看注释1处给intent设置了Intent.FLAG_ACTIVITY_NEW_TASK(详情查看浅析Activity启动模式),这样我们启动的应用根Activity就会在一个新的栈中创建。看29行注释2处调用了startActivity()方法,Launcher实际上是继承Activity的,这里的startActivity()方法就是Activity里的,最后会走到startActivityForResult()方法中。

Activity.startActivityForResult()

public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,           @Nullable Bundle options) {       if (mParent == null) {           options = transferSpringboardActivityOptions(options);           Instrumentation.ActivityResult ar =               mInstrumentation.execStartActivity(                   this, mMainThread.getApplicationThread(), mToken, this,                   intent, requestCode, options);           ...       } else {           ...       }   }

我们主要看5~8行代码,调用Instrumentation类的execStartActivity()方法。

Instrumentation.execStartActivity()

public ActivityResult execStartActivity(    Context who, IBinder contextThread, IBinder token, String target,    Intent intent, int requestCode, Bundle options) {    IApplicationThread whoThread = (IApplicationThread) contextThread;    if (mActivityMonitors != null) {        synchronized (mSync) {            final int N = mActivityMonitors.size();            for (int i=0; ifinal ActivityMonitor am = mActivityMonitors.get(i);                ActivityResult result = null;                if (am.ignoreMatchingSpecificIntents()) {                    result = am.onStartActivity(intent);                }                if (result != null) {                    am.mHits++;                    return result;                } else if (am.match(who, null, intent)) {                    am.mHits++;                    if (am.isBlocking()) {                        return requestCode >= 0 ? am.getResult() : null;                    }                    break;                }            }        }    }    try {        intent.migrateExtraStreamToClipData();        intent.prepareToLeaveProcess(who);        int result = ActivityManager.getService()            .startActivity(whoThread, who.getBasePackageName(), intent,                    intent.resolveTypeIfNeeded(who.getContentResolver()),                    token, target, requestCode, 0, null, options);        checkStartActivityResult(result, intent);    } catch (RemoteException e) {        throw new RuntimeException("Failure from system", e);    }    return null;}

查看第30行,ActivityManager.getService().startActivity(),我们先来看看ActivityManager.getService()

ActivityManager.getService()

public static IActivityManager getService() {    return IActivityManagerSingleton.get();}private static final Singleton IActivityManagerSingleton =        new Singleton() {            @Override            protected IActivityManager create() {                final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);// 注释1                final IActivityManager am = IActivityManager.Stub.asInterface(b); // 注释2                return am;            }        };

通过上面代码,我们可以知道getService()实际上是通过Singleton类获取IActivityManager。注释1处获取到IBinder类型的ActivityManagerService的引用。

SystemServer中调用ActivityManagerService.setSystemProcess(),在setSystemProcess()方法中可以找到以下内容:

ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);

没错会将ActivityManagerService存储到ServiceManager中进行管理。也就是注释1处通过Context.ACTIVITY_SERVICE查找到IBinder类型的AMS引用。

我们再接着看注释2处,看到这里你肯定很清楚,这是AIDL实现(如果不了解AIDL可以点击查看我的另一篇文章),IActivityManager.java会在编译时生成,Stub是其内部类,调用其asInterface()方法会将IBinder转换成IActivityManager接口,如果是跨进程的会为其生成代理Stub.ProxyActivityManagerService继承IActivityManager.Stub

注意:
实际上在这里使用AIDL方式是在Android 8.0Android 8.0之前使用的是代理,在Instrumentation.execStartActivity()方法中使用ActivityManagerNative.getDefault().startActivity()ActivityManagerNative.getDefault()返回ActivityManagerProxy

再回到Instrumentation.execStartActivity()方法中,通过上面解析,实际上最后调用的ActivityManagerServicestartActivity()方法。

ActivityManagerService.startActivty()

@Overridepublic final int startActivity(IApplicationThread caller, String callingPackage,        Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,        int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {    return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,            resultWho, requestCode, startFlags, profilerInfo, bOptions,            UserHandle.getCallingUserId());}

接着调用startActivityAsUser()

ActivityManagerService.startActivityAsUser()

@Overridepublic final int startActivityAsUser(IApplicationThread caller, String callingPackage,        Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,        int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {    enforceNotIsolatedCaller("startActivity");//判断调用者进程是否被隔离    //检查调用者权限    userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),            userId, false, ALLOW_FULL_ONLY, "startActivity", null);    // TODO: Switch to user app stacks here.    return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent,            resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,            profilerInfo, null, null, bOptions, false, userId, null, "startActivityAsUser");}

ActivityStarter.startActivityMayWait()

final int startActivityMayWait(IApplicationThread caller, int callingUid,        String callingPackage, Intent intent, String resolvedType,        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,        IBinder resultTo, String resultWho, int requestCode, int startFlags,        ProfilerInfo profilerInfo, WaitResult outResult,        Configuration globalConfig, Bundle bOptions, boolean ignoreTargetSecurity, int userId,        TaskRecord inTask, String reason) {        ...        // Save a copy in case ephemeral needs it        final Intent ephemeralIntent = new Intent(intent);        // Don't modify the client's object!        intent = new Intent(intent);        ...        ResolveInfo rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId);        ...        // Collect information about the target of the Intent.        ActivityInfo aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, profilerInfo);        ...        int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,                aInfo, rInfo, voiceSession, voiceInteractor,                resultTo, resultWho, requestCode, callingPid,                callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,                options, ignoreTargetSecurity, componentSpecified, outRecord, inTask,                reason);        ...        return res;    }}

startActivityManyWait()方法代码过长,这里只保留我们想要的,接着往下看。

ActivityStarter.startActivityLocked()

int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,            ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,            ActivityRecord[] outActivity, TaskRecord inTask, String reason) {...        mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,                aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,                callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,                options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,                inTask);...        return mLastStartActivityResult != START_ABORTED ? mLastStartActivityResult : START_SUCCESS;    }

ActivityStarter.startActivity()

private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,        String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,        IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,        String callingPackage, int realCallingPid, int realCallingUid, int startFlags,        ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,        ActivityRecord[] outActivity, TaskRecord inTask) {    int err = ActivityManager.START_SUCCESS;    // Pull the optional Ephemeral Installer-only bundle out of the options early.    final Bundle verificationBundle            = options != null ? options.popAppVerificationBundle() : null;    ProcessRecord callerApp = null;    if (caller != null) {        // 获取调用者进程的记录对象        callerApp = mService.getRecordForAppLocked(caller);        if (callerApp != null) {            callingPid = callerApp.pid;            callingUid = callerApp.info.uid;        } else {            Slog.w(TAG, "Unable to find app for caller " + caller                    + " (pid=" + callingPid + ") when starting: "                    + intent.toString());            err = ActivityManager.START_PERMISSION_DENIED;        }    }    final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;    if (err == ActivityManager.START_SUCCESS) {        Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)                + "} from uid " + callingUid);    }    ActivityRecord sourceRecord = null;    ActivityRecord resultRecord = null;    if (resultTo != null) {        //获取调用者所在Activity记录        sourceRecord = mSupervisor.isInAnyStackLocked(resultTo);        if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,                "Will send result to " + resultTo + " " + sourceRecord);        if (sourceRecord != null) {            if (requestCode >= 0 && !sourceRecord.finishing) {                resultRecord = sourceRecord;            }        }    }    final int launchFlags = intent.getFlags();    if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {        //将Activity执行结果从源Activity转移到正在启动的新Activity,不需要返回结果则不会进入该分支        ...    }    // error 状态判断检查    boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,            requestCode, callingPid, callingUid, callingPackage, ignoreTargetSecurity, callerApp,            resultRecord, resultStack, options);    //权限检查    ...    // 创建Activity记录对象    ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,            callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),            resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,            mSupervisor, options, sourceRecord);    ...    final ActivityStack stack = mSupervisor.mFocusedStack;    if (voiceSession == null && (stack.mResumedActivity == null            || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {        if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,                realCallingPid, realCallingUid, "Activity start")) {            PendingActivityLaunch pal =  new PendingActivityLaunch(r,                    sourceRecord, startFlags, stack, callerApp);            mPendingActivityLaunches.add(pal);            ActivityOptions.abort(options);            return ActivityManager.START_SWITCHES_CANCELED;        }    }    ...    ////处理 pendind Activity的启动      doPendingActivityLaunchesLocked(false);    return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,            options, inTask, outActivity);}

ActivityStarter.startActivty()

private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,        int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,        ActivityRecord[] outActivity) {    int result = START_CANCELED;    try {        mService.mWindowManager.deferSurfaceLayout();        result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,                startFlags, doResume, options, inTask, outActivity);    } finally {        ...    }...    return result;}

ActivityStarter.startActivityUnChecked()

private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,        int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,        ActivityRecord[] outActivity) {    ...    // 如果正在启动的活动与当前位于顶部的活动相同,那么我们需要检查它是否应该只启动一次。    final ActivityStack topStack = mSupervisor.mFocusedStack;    final ActivityRecord topFocused = topStack.topActivity();    final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop);    final boolean dontStart = top != null && mStartActivity.resultTo == null            && top.realActivity.equals(mStartActivity.realActivity)            && top.userId == mStartActivity.userId            && top.app != null && top.app.thread != null            && ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0            || mLaunchSingleTop || mLaunchSingleTask);    if (dontStart) {        // 确保我们已经正确地恢复了顶部的Activity。        topStack.mLastPausedActivity = null;        if (mDoResume) {            mSupervisor.resumeFocusedStackTopActivityLocked();        }        ActivityOptions.abort(mOptions);        if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {            // We don't need to start a new activity, and the client said not to do            // anything if that is the case, so this is it!            return START_RETURN_INTENT_TO_CALLER;        }        deliverNewIntent(top);        // 不要使用mStartActivity.task来显示吐司。 我们不是开始一项新活动,而是重新放到顶部。 mStartActivity中的字段可能未完全初始化。        mSupervisor.handleNonResizableTaskIfNeeded(top.getTask(), preferredLaunchStackId,                preferredLaunchDisplayId, topStack.mStackId);        return START_DELIVERED_TO_TOP;    }    boolean newTask = false;    final TaskRecord taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)            ? mSourceRecord.getTask() : null;    // Should this be considered a new task?    int result = START_SUCCESS;    if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask            && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {        newTask = true;        // 创建新的栈        result = setTaskFromReuseOrCreateNewTask(                taskToAffiliate, preferredLaunchStackId, topStack);    } else if (mSourceRecord != null) {        result = setTaskFromSourceRecord();    } else if (mInTask != null) {        result = setTaskFromInTask();    } else {        setTaskToCurrentTopOrCreateNewTask();    }    if (result != START_SUCCESS) {        return result;    }    ...    if (mDoResume) {        final ActivityRecord topTaskActivity =                mStartActivity.getTask().topRunningActivityLocked();        if (!mTargetStack.isFocusable()                || (topTaskActivity != null && topTaskActivity.mTaskOverlay                && mStartActivity != topTaskActivity)) {            ...        } else {            if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {                mTargetStack.moveToFront("startActivityUnchecked");            }            mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,                    mOptions);        }    } else {        mTargetStack.addRecentActivityLocked(mStartActivity);    }    ...    return START_SUCCESS;}

ActivityStackSupervisor.resumeFocusedStackTopActivityLocked()

boolean resumeFocusedStackTopActivityLocked(        ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {    if (!readyToResume()) {        return false;    }    // 目标栈拥有焦点    if (targetStack != null && isFocusedStack(targetStack)) {        //确保恢复堆栈中的顶部活动        return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);    }    //获取要启动的Activity所在栈的栈顶的处于活动状态的ActivityRecord    final ActivityRecord r = mFocusedStack.topRunningActivityLocked();    if (r == null || r.state != RESUMED) {        mFocusedStack.resumeTopActivityUncheckedLocked(null, null);    } else if (r.state == RESUMED) {        // Kick off any lingering app transitions form the MoveTaskToFront operation.        mFocusedStack.executeAppTransition(targetOptions);    }    return false;}

ActivityStack.resumeTopActivityUncheckedLocked()

boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {    ...    result = resumeTopActivityInnerLocked(prev, options);    ...    return result;}

ActivityStack.resumeTopActivityInnerLocked()

private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {     ...     mStackSupervisor.startSpecificActivityLocked(next, true, true);     ...     return true;}

ActivityStackSupervisor.startSpecificActivityLocked()

void startSpecificActivityLocked(ActivityRecord r,        boolean andResume, boolean checkConfig) {    // 获取启动Activity所在进程    ProcessRecord app = mService.getProcessRecordLocked(r.processName,            r.info.applicationInfo.uid, true);    r.getStack().setLaunchTime(r);    // 判断进程是否已运行    if (app != null && app.thread != null) {        try {            if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0                    || !"android".equals(r.info.packageName)) {                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);        }    }    mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,            "activity", r.intent.getComponent(), false, false, true);}

ActivityStackSupervisor. realStartActivityLocked()

final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,          boolean andResume, boolean checkConfig) throws RemoteException {    ...    app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,          System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),          new Configuration(task.mOverrideConfig), r.compat, r.launchedFromPackage,          task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,          newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);    ...          return true;}

其中app.threadIApplicationThreadIApplicationThread也是AIDL的方式,其实现类为ActivityThread的内部类ApplicationThreadApplicationThread继承IApplicationThread.Stub

ApplicationThread.scheduleLaunchActivity()

public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,        ActivityInfo info, Configuration curConfig, Configuration overrideConfig,        CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,        int procState, Bundle state, PersistableBundle persistentState,        List pendingResults, List pendingNewIntents,        boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {    updateProcessState(procState, false);    ActivityClientRecord r = new ActivityClientRecord();    r.token = token;    r.ident = ident;    ...    updatePendingConfiguration(curConfig);    sendMessage(H.LAUNCH_ACTIVITY, r);}

scheduleLaunchActivity()对相关数据进行封装,然后通过调用ActivityThread类的sendMessage()发送出去。ApplicationThreadActivityThread内部类,拥有操作ActivityThread的所有属性和方法的权限。

ActivityThread.sendMessage()

final H mH = new H();private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {    ...    Message msg = Message.obtain();    msg.what = what;    msg.obj = obj;    msg.arg1 = arg1;    msg.arg2 = arg2;    if (async) {        msg.setAsynchronous(true);    }    mH.sendMessage(msg);}

mH就是H类的实例化对象,接着我们看看H类具体是什么。

ActivityThread.H

    private class H extends Handler {        public static final int LAUNCH_ACTIVITY         = 100;        public static final int PAUSE_ACTIVITY          = 101;        public static final int PAUSE_ACTIVITY_FINISHING= 102;        public static final int STOP_ACTIVITY_SHOW      = 103;        ...        public void handleMessage(Message msg) {            switch (msg.what) {                case LAUNCH_ACTIVITY: {                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");                    final ActivityClientRecord r = (ActivityClientRecord) msg.obj;                    r.loadedApk = getLoadedApkNoCheck(                            r.activityInfo.applicationInfo, r.compatInfo);                    handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);                } break;                ...            }            Object obj = msg.obj;            if (obj instanceof SomeArgs) {                ((SomeArgs) obj).recycle();            }        }    }

根据上面的代码可知H继承Handler,重写其handleMessage()方法,根据不同的消息状态码分发消息。

前面代码sendMessage(H.LAUNCH_ACTIVITY, r);,会进入LAUNCH_ACTIVITY流程,调用ActivityThread.handleLaunchActivity()方法。

ActivityThread.handleLaunchActivity()

    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {        ...        Activity a = performLaunchActivity(r, customIntent);        if (a != null) {            r.createdConfig = new Configuration(mConfiguration);            reportSizeConfigurations(r);            Bundle oldState = r.state;            handleResumeActivity(r.token, false, r.isForward,                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);            ...        } else {           ...        }    }

handleLaunchActivity()方法中通过performLaunchActivity()方法来创建要启动的Activity,通过handleResumeActivity()方法来启动ActivityonResume()生命周期方法。

ActivityThread.performLaunchActivity()

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {        // 从ActivityClientRecord中取出要启动Activity的信息        ActivityInfo aInfo = r.activityInfo;        if (r.loadedApk == null) {            r.loadedApk = getLoadedApk(aInfo.applicationInfo, r.compatInfo,                    Context.CONTEXT_INCLUDE_CODE);        }        ComponentName component = r.intent.getComponent();        if (component == null) {            component = r.intent.resolveActivity(                mInitialApplication.getPackageManager());            r.intent.setComponent(component);        }        if (r.activityInfo.targetActivity != null) {            component = new ComponentName(r.activityInfo.packageName,                    r.activityInfo.targetActivity);        }        // 创建ContextImpl        ContextImpl appContext = createBaseContextForActivity(r);        Activity activity = null;        try {            //获取ClassLoader            java.lang.ClassLoader cl = appContext.getClassLoader();            // 通过classLoader.loadClass(className).newInstance()创建Activity            activity = mInstrumentation.newActivity(                    cl, component.getClassName(), r.intent);            StrictMode.incrementExpectedActivityCount(activity.getClass());            r.intent.setExtrasClassLoader(cl);            r.intent.prepareToEnterProcess();            if (r.state != null) {                r.state.setClassLoader(cl);            }        } catch (Exception e) {            ...        }        try {            // 创建Application对象            Application app = r.loadedApk.makeApplication(false, mInstrumentation);                ...            if (activity != null) {                ...                // 调用Activity的attach()方法进行数据初始化,ContextImpl和Activity关联,Activity和window关联                activity.attach(appContext, this, getInstrumentation(), r.token,                        r.ident, app, r.intent, r.activityInfo, title, r.parent,                        r.embeddedID, r.lastNonConfigurationInstances, config,                        r.referrer, r.voiceInteractor, window, r.configCallback);                ...                // 调用callActivityOnCreate,其内部调用Activity的performCreate(),从而调用onCreate()                if (r.isPersistable()) {                    mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);                } else {                    mInstrumentation.callActivityOnCreate(activity, r.state);                }                ...            }            ...            mActivities.put(r.token, r);        } catch (SuperNotCalledException e) {            ...        } catch (Exception e) {            ...        }        return activity;    }

Instrumentation.callActivityOnCreate()

public void callActivityOnCreate(Activity activity, Bundle icicle) {    prePerformCreate(activity);    activity.performCreate(icicle);    postPerformCreate(activity);}

Activity.performCreate()

final void performCreate(Bundle icicle, PersistableBundle persistentState) {    mCanEnterPictureInPicture = true;    restoreHasCurrentPermissionRequest(icicle);    if (persistentState != null) {        onCreate(icicle, persistentState);    } else {        onCreate(icicle);    }    ...}

写在后面

Activity工作原理相关代码是比较复杂的,代码量也比较庞大,在这里也只是分析了Activity启动过程的大致流程,很多细节并没有分析到。只能说学习在路上!

更多相关文章

  1. Android 窗帘(Curtain Menu)效果五之应用场景和使用方法
  2. android 获取路径目录方法 以及 获取路径方法
  3. Linux系统下安装android sdk的方法步骤
  4. 选项卡使用方法二(Android学习随笔十三)
  5. 使用AndroidStudio编译NDK的方法及错误解决方式
  6. Android Studio 配置快捷方式生成JNI头文件的方法
  7. android 读取DDMS里的文件时打不开,解决方法
  8. Android:Manifest merger failed with multiple errors, see log
  9. android中真正destroy掉activity的方法

随机推荐

  1. 我从Android交易市场一天赚到250美元的故
  2. Android系统版本与AspectJ
  3. (4.2.2.2)【android开源工具】【Android(安
  4. Android高性能编码 - 第二篇 数据库操作
  5. 蚊子132 需要了解Android(安卓)SDK支持的
  6. Android第十九期 - Sqlite离线存储教你写
  7. 【设计模式与Android】原型模式——复制
  8. 基于jquery的小记事本,及$.get/$.post/$.a
  9. 第三篇 android 应用开发模式之MVC模式及
  10. Android"挂逼"修炼之行---微信摇骰子和猜