Activity启动流程

如何启动一个Activity

启动一个activity要用startSActivity()方法,所以首先先从这里看起:

 /**
 * Same as {@link #startActivity(Intent, Bundle)} with no options
 * specified.
 *
 * @param intent The intent to start.
 *
 * @throws android.content.ActivityNotFoundException
 *
 * @see #startActivity(Intent, Bundle)
 * @see #startActivityForResult
 */
@Override
public void startActivity(Intent intent) {
    this.startActivity(intent, null);
}  

我们看到它会调用自身的startActivity(intent,null):

/**
 * Launch a new activity.  You will not receive any information about when
 * the activity exits.  This implementation overrides the base version,
 * providing information about
 * the activity performing the launch.  Because of this additional
 * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not
 * required; if not specified, the new activity will be added to the
 * task of the caller.
 *
 * 

This method throws {@link android.content.ActivityNotFoundException} * if there was no Activity found to run the given Intent. * * @param intent The intent to start. * @param options Additional options for how the Activity should be started. * See {@link android.content.Context#startActivity(Intent, Bundle)} * Context.startActivity(Intent, Bundle)} for more details. * * @throws android.content.ActivityNotFoundException * * @see #startActivity(Intent) * @see #startActivityForResult */ @Override public void startActivity(Intent intent, @Nullable Bundle options) { if (options != null) { startActivityForResult(intent, -1, options); } else { // Note we want to go through this call for compatibility with // applications that may have overridden the method. startActivityForResult(intent, -1); } }

我们看到注释上说的是会启动一个新的activity,然后会调用startActivityForRorResult():

/**
 * Launch an activity for which you would like a result when it finished.
 * When this activity exits, your
 * onActivityResult() method will be called with the given requestCode.
 * Using a negative requestCode is the same as calling
 * {@link #startActivity} (the activity is not launched as a sub-activity).
 *
 * 

Note that this method should only be used with Intent protocols * that are defined to return a result. In other protocols (such as * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may * not get the result when you expect. For example, if the activity you * are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not * run in your task and thus you will immediately receive a cancel result. * *

As a special case, if you call startActivityForResult() with a requestCode * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your * activity, then your window will not be displayed until a result is * returned back from the started activity. This is to avoid visible * flickering when redirecting to another activity. * *

This method throws {@link android.content.ActivityNotFoundException} * if there was no Activity found to run the given Intent. * * @param intent The intent to start. * @param requestCode If >= 0, this code will be returned in * onActivityResult() when the activity exits. * @param options Additional options for how the Activity should be started. * See {@link android.content.Context#startActivity(Intent, Bundle)} * Context.startActivity(Intent, Bundle)} for more details. * * @throws android.content.ActivityNotFoundException * * @see #startActivity */ 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); if (ar != null) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0) { // If this start is requesting a result, we can avoid making // the activity visible until the result is received. Setting // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the // activity hidden during this time, to avoid flickering. // This can only be done when a result is requested because // that guarantees we will get information back when the // activity is finished, no matter what happens to it. mStartedActivity = true; } cancelInputsAndStartExitTransition(options); // TODO Consider clearing/flushing other event sources and events for child windows. } else { if (options != null) { mParent.startActivityFromChild(this, intent, requestCode, options); } else { // Note we want to go through this method for compatibility with // existing applications that may have overridden it. mParent.startActivityFromChild(this, intent, requestCode); } } }

看这段代码,我们可以看到核心代码是:

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

在这里我们注意一下mMainThread.getApplicationThread,它的类型是ApplicationThread,ApplicationThread是ActivityThread的一个内部类。
我们继续往下看:

 public ActivityResult execStartActivity(
        Context who, IBinder contextThread, IBinder token, Activity 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; i= 0 ? am.getResult() : null;
                   }
                    break;
                }
            }
        }
    }
    try {
        intent.migrateExtraStreamToClipData();
        intent.prepareToLeaveProcess();
        int result = ActivityManagerNative.getDefault()
           .startActivity(whoThread, who.getBasePackageName(), intent,
                    intent.resolveTypeIfNeeded(who.getContentResolver()),
                    token, target != null ? target.mEmbeddedID : null,
                    requestCode, 0, null, options);
        checkStartActivityResult(result, intent);
    } catch (RemoteException e) {
    }
   return null;
}

我们可以看到核心代码是:

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

我们可以看出来,启动activity真正的实现是由ActivityManagerNative.getDefault().startActivity方法来完成的。ActivityManagerService继承自ActivityManagerNative,ActivityManagerNattive继承自Binder并实现了IActivityManager这个Binder接口,因此ActivityManagerService也是一个Binder,是IActivityManager的具体实现。

public abstract class ActivityManagerNative extends Binder implements IActivityManager

ActivityManagerNative.getDefault()得到的是IActivityManager,在ActivityManagerNative中采用单例模式对外提供。

 static public IActivityManager getDefault() {
      return gDefault.get();
  }

private static final Singleton gDefault = new Singleton() {
    protected IActivityManager create() {
        IBinder b = ServiceManager.getService("activity");
        if (false) {
            Log.v("ActivityManager", "default service binder = " + b);
        }
        IActivityManager am = asInterface(b);
        if (false) {
            Log.v("ActivityManager", "default service = " + am);
        }
        return am;
    }
};

通过上面分析,我们可以指定真正启动Activity的是IActivityManager,ActivityManagerService是它的实现类,于是就是ActivityManagerService来启动Activity的,于是我们来分析ActivityManagerService的startActivity方法:

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

 @Override
 public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
         Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
         int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) {
    enforceNotIsolatedCaller("startActivity");
     userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
             false, ALLOW_FULL_ONLY, "startActivity", null);
     // TODO: Switch to user app stacks here.
     return mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent,
             resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
             profilerInfo, null, null, options, userId, null, null);
 }

我们可以看到,Activity的启动过程又转到了ActivityStackSupervisor的startActivityMayWait方法中,在这个方面中又调用了startActivityLocked方法,然后在startActivityLocked方法中会进行权限的检验,并调用startActivityUncheckedLocked方法,接着在startActivityUncheckedLocked方法中调用resumeTopActivitiesLocked方法,

boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
        Bundle targetOptions) {
    if (targetStack == null) {
        targetStack = getFocusedStack();
    }
    // Do targetStack first.
    boolean result = false;
    if (isFrontStack(targetStack)) {
        result = targetStack.resumeTopActivityLocked(target, targetOptions);
    }
    for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
        final ArrayList stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
        for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
            final ActivityStack stack = stacks.get(stackNdx);
            if (stack == targetStack) {
                // Already started above.
                continue;
            }
            if (isFrontStack(stack)) {
                stack.resumeTopActivityLocked(null);
           }
        }
    }
    return result;
}

然后它调用了ActivityStack中的resumeTopActivityLocked方法,于是就从ActivityStackSupervisor转移到了ActivityStack中。 在ActivityStack的resumeTopActivityLocked方法中,又调用了resumeTopActivityInnerLocked方法,在这个方法中又调用了ActivityStackSupervisor的startSpecificActivityLocked方法,又回到了ActivityStackSupervisor中。

void startSpecificActivityLocked(ActivityRecord r,
       boolean andResume, boolean checkConfig) {
    // 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.
    }

    mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
            "activity", r.intent.getComponent(), false, false, true);
}

我们可以看到,它又调用了realStartActivityLocked(r, app, andResume, checkConfig); 在这个方法中有这样一段代码:

    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,
               mService.isNextTransitionForward(), profilerInfo);  

这段代码很重要,其中app.thread的类型为IApplicationThread,我们看看它的声明:

 public interface IApplicationThread extends IInterface {
 void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving,
         int configChanges, boolean dontReport) throws RemoteException;
 void scheduleStopActivity(IBinder token, boolean showWindow,
         int configChanges) throws RemoteException;
 void scheduleWindowVisibility(IBinder token, boolean showWindow) throws RemoteException;
 void scheduleSleeping(IBinder token, boolean sleeping) throws RemoteException;
 void scheduleResumeActivity(IBinder token, int procState, boolean isForward, Bundle resumeArgs)
         throws RemoteException;
 void scheduleSendResult(IBinder token, List results) throws RemoteException;
 void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
         ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
         String referrer, IVoiceInteractor voiceInteractor, int procState, Bundle state,
         PersistableBundle persistentState, List pendingResults,
         List pendingNewIntents, boolean notResumed, boolean isForward,
         ProfilerInfo profilerInfo) throws RemoteException;
 void scheduleRelaunchActivity(IBinder token, List pendingResults,
         List pendingNewIntents, int configChanges,
         boolean notResumed, Configuration config) throws RemoteException;
 void scheduleNewIntent(List intent, IBinder token) throws RemoteException;
 void scheduleDestroyActivity(IBinder token, boolean finished,
         int configChanges) throws RemoteException;
 void scheduleReceiver(Intent intent, ActivityInfo info, CompatibilityInfo compatInfo,
         int resultCode, String data, Bundle extras, boolean sync,
         int sendingUser, int processState) throws RemoteException;
 static final int BACKUP_MODE_INCREMENTAL = 0;
 static final int BACKUP_MODE_FULL = 1;
 static final int BACKUP_MODE_RESTORE = 2;
 static final int BACKUP_MODE_RESTORE_FULL = 3;
 void scheduleCreateBackupAgent(ApplicationInfo app, CompatibilityInfo compatInfo,
         int backupMode) throws RemoteException;
 void scheduleDestroyBackupAgent(ApplicationInfo app, CompatibilityInfo compatInfo)
         throws RemoteException;
 void scheduleCreateService(IBinder token, ServiceInfo info,
         CompatibilityInfo compatInfo, int processState) throws RemoteException;
 void scheduleBindService(IBinder token,
         Intent intent, boolean rebind, int processState) throws RemoteException;
 void scheduleUnbindService(IBinder token,
         Intent intent) throws RemoteException;
 void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
         int flags, Intent args) throws RemoteException;
 void scheduleStopService(IBinder token) throws RemoteException;
 static final int DEBUG_OFF = 0;
 static final int DEBUG_ON = 1;
 static final int DEBUG_WAIT = 2;
 void bindApplication(String packageName, ApplicationInfo info, List providers,
         ComponentName testName, ProfilerInfo profilerInfo, Bundle testArguments,
         IInstrumentationWatcher testWatcher, IUiAutomationConnection uiAutomationConnection,
         int debugMode, boolean openGlTrace, boolean restrictedBackupMode, boolean persistent,
        Configuration config, CompatibilityInfo compatInfo, Map services,
        Bundle coreSettings) throws RemoteException;

它继承了IInterface接口,所以它是一个BInder类型的接口,我们可以看到,它有很多关于启动,停止Activity和Service的接口,它的实现类是ActivityThread中的内部类ApplicationThread。

 private class ApplicationThread extends ApplicationThreadNative {
 public abstract class More ...ApplicationThreadNative extends Binder
     implements IApplicationThread {

可以看出,ApplicationThread继承了ApplicationThreadNative,而ApplicationThreadNative继承了Bionder并实现了IApplicationThread接口。通过分析可以发现,ApplicationThreadNative的作用和系统为AIDL文件生成的类是一样的。
在ApplicationThreadNative中,有个内部类,ApplicationThreadProxy类,这个内部类是系统为AIDL文件自动生成的代理类

class ApplicationThreadProxy implements IApplicationThread

饶了一大圈,其实我们又回到了ApplicationThread中,ApplicationThread通过scheduleLaunchActivity方法来启动Activity

// we use token to identify this activity without having to send the
     // activity itself back to the activity manager. (matters more with ipc)
     public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
             ActivityInfo info, Configuration curConfig, 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;
         r.intent = intent;
         r.referrer = referrer;
         r.voiceInteractor = voiceInteractor;
         r.activityInfo = info;
         r.compatInfo = compatInfo;
         r.state = state;
         r.persistentState = persistentState;

         r.pendingResults = pendingResults;
         r.pendingIntents = pendingNewIntents;

        r.startsNotResumed = notResumed;
         r.isForward = isForward;

         r.profilerInfo = profilerInfo;

         updatePendingConfiguration(curConfig);

        sendMessage(H.LAUNCH_ACTIVITY, r);
     }

我们可以看到,其实就是发送了一个启动Activity的消息交给Handler处理

 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
    if (DEBUG_MESSAGES) Slog.v(
        TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
        + ": " + arg1 + " / " + obj);
    Message msg = Message.obtain();
    msg.what = what;
    msg.obj = obj;
    msg.arg1 = arg1;
    msg.arg2 = arg2;
    if (async) {
        msg.setAsynchronous(true);
    }
    mH.sendMessage(msg);
}

这个Handler的名字叫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 static final int STOP_ACTIVITY_HIDE      = 104;
    public static final int SHOW_WINDOW             = 105;
    public static final int HIDE_WINDOW             = 106;
    public static final int RESUME_ACTIVITY         = 107;
    public static final int SEND_RESULT             = 108;
    public static final int DESTROY_ACTIVITY        = 109;
    public static final int BIND_APPLICATION        = 110;
    public static final int EXIT_APPLICATION        = 111;
    public static final int NEW_INTENT              = 112;
    public static final int RECEIVER                = 113;
    public static final int CREATE_SERVICE          = 114;
    public static final int SERVICE_ARGS            = 115;
    public static final int STOP_SERVICE            = 116;

    ... ...

   public void  handleMessage(Message msg) {
       if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
        switch (msg.what) {
            case LAUNCH_ACTIVITY: {
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
                final ActivityClientRecord r = (ActivityClientRecord) msg.obj;

                r.packageInfo = getPackageInfoNoCheck(
                        r.activityInfo.applicationInfo, r.compatInfo);
                handleLaunchActivity(r, null);
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
            } break;
            case RELAUNCH_ACTIVITY: {
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
                ActivityClientRecord r = (ActivityClientRecord)msg.obj;
                handleRelaunchActivity(r);
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
            } break;
            case PAUSE_ACTIVITY:
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
                handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2,
                        (msg.arg1&2) != 0);
                maybeSnapshot();
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                break;
            case PAUSE_ACTIVITY_FINISHING:
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
                handlePauseActivity((IBinder)msg.obj, true, (msg.arg1&1) != 0, msg.arg2,
                        (msg.arg1&1) != 0);
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                break;
            case STOP_ACTIVITY_SHOW:
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
                handleStopActivity((IBinder)msg.obj, true, msg.arg2);
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                break;
           case STOP_ACTIVITY_HIDE:
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
                handleStopActivity((IBinder)msg.obj, false, msg.arg2);
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                break;
            ... ...

        }
        if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
    }

我们可以看到它最终调用了ActivityThread的handleLaunchActivity方法来实现:

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    // If we are getting ready to gc after going to the background, well
    // we are back active so skip it.
    unscheduleGcIdler();
    mSomeActivitiesChanged = true;

    if (r.profilerInfo != null) {
        mProfiler.setProfiler(r.profilerInfo);
        mProfiler.startProfiling();
    }

    // Make sure we are running with the most recent config.
    handleConfigurationChanged(null, null);

    if (localLOGV) Slog.v(
        TAG, "Handling launch of " + r);

    // Initialize before creating the activity
   WindowManagerGlobal.initialize();

    Activity a = performLaunchActivity(r, customIntent);

    if (a != null) {
        r.createdConfig = new Configuration(mConfiguration);
        Bundle oldState = r.state;
        handleResumeActivity(r.token, false, r.isForward,
                !r.activity.mFinished && !r.startsNotResumed);
        ... ...
    }
    ... ...
}

我们可以看到performLaunchActivity方法最终完成了Activity对象的创建和启动过程,并且ActivityThread通过handleResumeActivity方法来调用被启动的activity 的onResume这一生命周期。

所以我们来看看performLaunchActivity完成了那些事:

  1. 从ActivityClientRecord中获取从待启动的Activity的组件信息:

    ActivityInfo aInfo = r.activityInfo;
    if (r.packageInfo == null) {
    r.packageInfo = getPackageInfo(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);
    }
    
  2. 使用Instrumentation的newActivity方法使用类加载器创建Activity对象:

     Activity activity = null;
    try {
        java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
        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) {
        if (!mInstrumentation.onException(activity, e)) {
            throw new RuntimeException(
                "Unable to instantiate activity " + component
                + ": " + e.toString(), e);
        }
    }
    
  3. 通过LoadedApk的makeApplication方法来尝试创建Application对象。

    public Application More ...makeApplication(boolean forceDefaultAppClass,
        Instrumentation instrumentation) {
     if (mApplication != null) {
         return mApplication;
     }
    
     Application app = null;
    
     String appClass = mApplicationInfo.className;
     if (forceDefaultAppClass || (appClass == null)) {
         appClass = "android.app.Application";
     }
    
     try {
         java.lang.ClassLoader cl = getClassLoader();
         if (!mPackageName.equals("android")) {
             initializeJavaContextClassLoader();
         }
         ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
         app = mActivityThread.mInstrumentation.newApplication(
                 cl, appClass, appContext);
         appContext.setOuterContext(app);
     } catch (Exception e) {
         if (!mActivityThread.mInstrumentation.onException(app, e)) {
             throw new RuntimeException(
                 "Unable to instantiate application " + appClass
                 + ": " + e.toString(), e);
         }
     }
     mActivityThread.mAllApplications.add(app);
     mApplication = app;
    
     if (instrumentation != null) {
         try {
             instrumentation.callApplicationOnCreate(app);
         } catch (Exception e) {
             if (!instrumentation.onException(app, e)) {
                 throw new RuntimeException(
                     "Unable to create application " + app.getClass().getName()
                     + ": " + e.toString(), e);
             }
         }
     }
    
     // Rewrite the R 'constants' for all library apks.
     SparseArray packageIdentifiers = getAssets(mActivityThread)
             .getAssignedPackageIdentifiers();
     final int N = packageIdentifiers.size();
     for (int i = 0; i < N; i++) {
         final int id = packageIdentifiers.keyAt(i);
         if (id == 0x01 || id == 0x7f) {
             continue;
         }
    
         rewriteRValues(getClassLoader(), packageIdentifiers.valueAt(i), id);
      }
    
         return app;
    }
    

    我们可以看到,如果Application被创建了,那么不会再重复创建,因此一个应用中只有一个Application对象。Application对象的创建也是通过Instrumentation来完成的,这个过程和activity对象的创建一样的,都是通过类加载器来实现的。Application创建完毕后,系统会通过Instrumentation的instrumentation.callApplicationOnCreate(app)方法来调用Application的onCreate方法。

  4. 创建ContextImpl对象并通过Activity的attach方法来完成一些重要数据的初始化。

     if (activity != null) {
            Context appContext = createBaseContextForActivity(r, activity);
            CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
            Configuration config = new Configuration(mCompatConfiguration);
            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
                   + r.activityInfo.name + " with config " + config);
            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);
    

    ContextImpl是一个很重要的数据结构,它是Context的具体实现,Context中的大部分逻辑都是由ContextImpl来完成的。ContextImpl是通过Activity的attach方法来和Activity建立关联的,除此以外,在attach方法中Activity还会完成Window的创建并建立自己和Window的关联,这个当Window接收到外部输入事件后就可以将事件传递给Activity。

    final void attach(Context context, ActivityThread aThread,
    Instrumentation instr, IBinder token, int ident,
    Application application, Intent intent, ActivityInfo info,
    CharSequence title, Activity parent, String id,
    NonConfigurationInstances lastNonConfigurationInstances,
    Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
    attachBaseContext(context);

    mFragments.attachActivity(this, mContainer, null);
    
    mWindow = PolicyManager.makeNewWindow(this);
    mWindow.setCallback(this);
    mWindow.setOnWindowDismissedCallback(this);
    mWindow.getLayoutInflater().setPrivateFactory(this);
    if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
        mWindow.setSoftInputMode(info.softInputMode);
    }
    if (info.uiOptions != 0) {
        mWindow.setUiOptions(info.uiOptions);
    }
    mUiThread = Thread.currentThread();
    
    mMainThread = aThread;
    mInstrumentation = instr;
    mToken = token;
    mIdent = ident;
    mApplication = application;
    mIntent = intent;
    mReferrer = referrer;
    mComponent = intent.getComponent();
    mActivityInfo = info;
    mTitle = title;
    mParent = parent;
    mEmbeddedID = id;
    mLastNonConfigurationInstances = lastNonConfigurationInstances;
    if (voiceInteractor != null) {
        if (lastNonConfigurationInstances != null) {
            mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
        } else {
            mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
                    Looper.myLooper());
        }
    }
    
    mWindow.setWindowManager(
            (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
            mToken, mComponent.flattenToString(),
            (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
    if (mParent != null) {
        mWindow.setContainer(mParent.getWindow());
    }
    mWindowManager = mWindow.getWindowManager();
    mCurrentConfig = config;
    

    }

  5. 调用Activity的onCreate方法:
    instrumentation.callApplicationOnCreate(app);由于activity的onCreate已经被调用,这也意味着Activity已经完成整个启动过程。

最后附上Activity启动流程的流程图:

我们知道在java中,main函数是一个程序的入口,那么android程序的入口在哪里呢?
答案是在ActivityThread中

 public static void main(String[] args) {
    SamplingProfilerIntegration.start();

    // CloseGuard defaults to true and can be quite spammy.  We
    // disable it here, but selectively enable it later (via
    // StrictMode) on debug builds, but using DropBox, not logs.
    CloseGuard.setEnabled(false);

    Environment.initForCurrentUser();

    // Set the reporter for event logging in libcore
    EventLogger.setReporter(new EventLoggingReporter());

    Security.addProvider(new AndroidKeyStoreProvider());

    // Make sure TrustedCertificateStore looks in the right place for CA certificates
    final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
    TrustedCertificateStore.setDefaultUserDirectory(configDir);

    Process.setArgV0("");

    Looper.prepareMainLooper();

    ActivityThread thread = new ActivityThread();
    thread.attach(false);

    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }

    if (false) {
        Looper.myLooper().setMessageLogging(new
                LogPrinter(Log.DEBUG, "ActivityThread"));
    }

    Looper.loop();

    throw new RuntimeException("Main thread loop unexpectedly exited");
}

在段代码中最核心的就是:

 Looper.prepareMainLooper();

    ActivityThread thread = new ActivityThread();
    thread.attach(false);

    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }

    if (false) {
        Looper.myLooper().setMessageLogging(new
                LogPrinter(Log.DEBUG, "ActivityThread"));
    }

    Looper.loop();

我们的UI线程就是在这里建立的。

最后为大家分享一张全面的启动流程:

参考:https://www.jianshu.com/p/9ecea420eb52

你可能感兴趣的:(android)