根Activity启动过程

Android应用程序启动(根Activity)过程 注释1:从 ServiceManager得到一个IBinder,也就是 “IBinder类型的AMS”注释2:将IBinder通过转换成一个 IActivityManager对象,通过get()可以得到这个对象。上面是AIDL的做法,用 进程间通信完成的从 客户端层到 服务端层的实现。8.0之前并不是AIDL的做法。至于AIDL的用法、原理网上已经有n多的blog去学习了。这里不再赘述。至此,第一阶段结束,方法最终会调用到。这部分其实特别好理解。 阅读详情

一、概述

从应用程序的角度出发,Activity的启动过程分为两种:一种是根Activity,另一种是子Activity。根Acitivity是launcher中以快捷图标的的形式启动,它的启动过程代表了一个Android应用程序的启动过程。子Activity由根Activity启动或有其他子Activity启动,它们可能与启动它们的Activity运行在同一个进程中也可能运行在不同的进程中,这取决于它们的配置和启动参数。

二、Launcher启动MainActivity的过程

  1. Launcher向ActivityManagerService发送一个启动MainActivity的进程间通信请求
  2. ActivityManagerService首先将要启动的MainActivity组件的信息保存下来,然后向Launcher组件发送一个暂停状态的进程间通信请求。
  3. Launcher组件进入暂停状态之后,就会向ActivityManagerService发送一个已进入暂停状态的进程间通信请求,以便ActivityManagerService可以继续执行启动MainActivity组件的操作。
  4. ActivityManagerService发现用来运行MainActivity组件的应用程序进程不存在,它就会启动一个新的应用程序进程。
  5. 新的应用程序进程启动完成之后,就会向ActivityManagerService发送一个启动完成的进程间通信请求,以便ActivityManagerService可以继续执行启动MainActivity组件的操作。
  6. ActivityManagerService将2步保存下来的MainActivity组件的信息发送给4创建的应用程序进程,以便可以将MainActivity组件启动起来。

三、源码分析

我们点击应用图标时,就会调用Launcher的startActivitySafely
packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
       ...
        //1、 Prepare intent
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        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())) {
                //2、 Could be launching some bookkeeping activity
                startActivity(intent, optsBundle);
            } 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;
    }

在注释1处设置Flag为Intent.FLAG_ACTIVITY_NEW_TASK,这样MainActivity就会在一个新的任务栈中启动,在注释2处调用父类Activity的成员函数启动参数为intent所描述Activity的组件。

   @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);
        }
    }

startActivityForResult的第二个参数为-1,Launcher不需要知道启动的结果,如果大于等于0就会在Activity退出时调用启动Activity的onActivityResult()方法。

public void startActivityForResult(Intent intent, int requestCode, 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());
            }
    }

mInstrumentation的类型为Instrumentation,调用Instrumentation的execStartActivity方法来执行启动MainActivity的操作。

Activity类的成员变量mMainThread的类型为ActivityThread,用来描述一个应用程序的进程。系统每当启动一个应用程序进程时,都会在加载一个ActivityThread类实例,并且会将这个ActivityThread类实例保存在每一个在该进程中启动的Activity组件的父类Activity的成员变量mMainThread中。Activity类的成员函数getApplicationThread()用来获取它内部的一个类型为ApplicationThread的Binder本地对象。通过execStartActivity()方法,可以将它传递给ActivityManagerService,这样ActivityManagerService就可以通过它来通知Launcher组件进入Paused状态。

public ActivityResult execStartActivity(
        Context who, IBinder contextThread, IBinder token, String target,
        Intent intent, int requestCode, Bundle options) {
        IApplicationThread whoThread = (IApplicationThread) contextThread;
        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;
    }

通过ActivityManager类的静态函数getService()来获取AMS的代理对象,接着调用startActivity方法

 public static IActivityManager getService() {
        return IActivityManagerSingleton.get();
    }

private static final Singleton<IActivityManager> IActivityManagerSingleton =
            new Singleton<IActivityManager>() {
                @Override
                protected IActivityManager create() {
                    final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
                    final IActivityManager am = IActivityManager.Stub.asInterface(b);
                    return am;
                }
            };

public abstract class Singleton<T> {
    private T mInstance;

    protected abstract T create();

    public final T get() {
        synchronized (this) {
            if (mInstance == null) {
                mInstance = create();
            }
            return mInstance;
        }
    }
}

第一次调用getService()方法时,它会通过Service Manager来获得一个“activity”的service引用,也就是IBinder类型的AMS引用,接着将其转化为IActivityManager类型的对象。由于AMS实现了IActivityManager.Stub接口,因此可以与AMS间进行进程间通讯。

以上过程都是Launcher进程中进行的。

下面的过程是在AMS中执行的

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

public 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());
    }

AMS的startActivity方法中return了startActivityAsUser方法,startActivityAsUser比startActivity多了一个UserHandle.getCallingUserId(),这个方法会获得调用者的UserId,AMS会根据这个UserId来确定调用者的权限。

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    public 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, null,
                "startActivityAsUser");
    }

调用ActivityStater的startActivityMayWait方法,倒数第二个参数类型为TaskRecord代表启动的Activity所在的栈,最后一个参数startActivityAsUser代表启动的理由。

frameworks/base/services/core/java/com/android/server/am/ActivityStarter.java

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,
            IActivityContainer iContainer, TaskRecord inTask, String reason) {
            ...
                
            final ActivityRecord[] outRecord = new ActivityRecord[1];
            int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
                    aInfo, rInfo, voiceSession, voiceInteractor,
                    resultTo, resultWho, requestCode, callingPid,
                    callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
                    options, ignoreTargetSecurity, componentSpecified, outRecord, container,
                    inTask, reason);
                    
            ...
            
            }
 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, ActivityStackSupervisor.ActivityContainer container,
            TaskRecord inTask, String reason) {

        if (TextUtils.isEmpty(reason)) {
            throw new IllegalArgumentException("Need to specify a reason.");
        }
        mLastStartReason = reason;
        mLastStartActivityTimeMs = System.currentTimeMillis();
        mLastStartActivityRecord[0] = null;

        mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
                aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
                callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
                options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
                container, inTask);

        return mLastStartActivityResult;
    }
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, ActivityStackSupervisor.ActivityContainer container,
            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) {
        // 1
            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;
            }
        }
        
        ...
        //注释2
        ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
                callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
                resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
                mSupervisor, container, options, sourceRecord);
        if (outActivity != null) {
            outActivity[0] = r;
        }
        
        return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,
                options, inTask, outActivity);
                
            
    }

每一个应用程序都使用一个ProcessRecord对象来描述,并且保存在ActivityManagerService内部,注释1处通过getRecordForAppLocked来获得与参数caller对应的一个ProcessRecord对象callerApp。注释2处创建一个ActivityRecord对象r来描述即将启动的Activity组件。

private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,//null
            ActivityRecord[] outActivity) {
        int result = START_CANCELED;
        try {
            mService.mWindowManager.deferSurfaceLayout();
            result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
                    startFlags, doResume, options, inTask, outActivity);
        } finally {
         // activity in an incomplete state can lead to issues, such as performing operations
            // without a window container.
            if (!ActivityManager.isStartResultSuccessful(result)
                    && mStartActivity.getTask() != null) {
                mStartActivity.getTask().removeActivity(mStartActivity);
            }
            mService.mWindowManager.continueSurfaceLayout();
        }
        return result;
    }

 private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
            ActivityRecord[] outActivity) {
            
            ...
            
             // Should this be considered a new task?
        int result = START_SUCCESS;
        if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
                && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
            //1
            newTask = true;
            result = setTaskFromReuseOrCreateNewTask(
                    taskToAffiliate, preferredLaunchStackId, topStack);
        } else if (mSourceRecord != null) {
            result = setTaskFromSourceRecord();
        } else if (mInTask != null) {
            result = setTaskFromInTask();
        } else {
            // This not being started from an existing activity, and not part of a new task...
            // just put it in the top task, though these days this case should never happen.
            setTaskToCurrentTopOrCreateNewTask();
        }
        if (result != START_SUCCESS) {
            return result;
        }
        
        ...
        
         if (mDoResume) {
            final ActivityRecord topTaskActivity =
                    mStartActivity.getTask().topRunningActivityLocked();
            if (!mTargetStack.isFocusable()
                    || (topTaskActivity != null && topTaskActivity.mTaskOverlay
                    && mStartActivity != topTaskActivity)) {
                // If the activity is not focusable, we can't resume it, but still would like to
                // make sure it becomes visible as it starts (this will also trigger entry
                // animation). An example of this are PIP activities.
                // Also, we don't want to resume activities in a task that currently has an overlay
                // as the starting activity just needs to be in the visible paused state until the
                // over is removed.
                mTargetStack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
                // Go ahead and tell window manager to execute app transition for this activity
                // since the app transition will not be triggered through the resume channel.
                mWindowManager.executeAppTransition();
            } else {
                // If the target stack was not previously focusable (previous top running activity
                // on that stack was not visible) then any prior calls to move the stack to the
                // will not update the focused stack.  If starting the new activity now allows the
                // task stack to be focusable, then ensure that we now update the focused stack
                // accordingly.
                if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {
                    mTargetStack.moveToFront("startActivityUnchecked");
                }
                //2
                mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
                        mOptions);
            }
        }

注释1默认情况下目标Activity与源Activity运行在同一任务中,如果启动组件标志值位设置FLAG_ACTIVITY_NEW_TASK,并且源Activity不需要目标Activity的运行结果,那么AMS就会将目标Activity运行在另一个不同的任务中。 调用注释2 resumeFocusedStackTopActivityLocked

frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java

boolean resumeFocusedStackTopActivityLocked(
            ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
        if (targetStack != null && isFocusedStack(targetStack)) {
            return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
        }
         //1
        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;
    }

在注释1处topRunningActivityLocked()获取要启动的Activity是否在栈顶。如果r == null || r.state != RESUMED就会调用resumeTopActivityUncheckedLocked()

 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
  // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
        // to be paused, while at the same time resuming the new resume activity only if the
        // previous activity can't go into Pip since we want to give Pip activities a chance to
        // enter Pip before resuming the next activity.
        final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
                && !lastResumedCanPip;

        boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
        //1
        if (mResumedActivity != null) {
            pausing |= startPausingLocked(userLeaving, false, next, false);
        }
        if (pausing && !resumeWhilePausing) {
            // 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);
            }
            return true;
        }
            
        }

由于当前resume状态的组件为Launcher,所以mResumedActivity != null为true,接下来就会调用startPausingLocked

final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
            ActivityRecord resuming, boolean pauseImmediately) {
               if (prev.app != null && prev.app.thread != null) {
            try {
                mService.updateUsageStats(prev, false);
                prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
                        userLeaving, prev.configChangeFlags, pauseImmediately);
            } catch (Exception e) {
                // Ignore exception, if process died other code will cleanup.
                Slog.w(TAG, "Exception thrown during pause", e);
                mPausingActivity = null;
                mLastPausedActivity = null;
                mLastNoHistoryActivity = null;
            }
        } 
    }

调用ThreadActivity的内部类ApplicationThread的schedulePauseActivity方法

 public final void schedulePauseActivity(IBinder token, boolean finished,
                boolean userLeaving, int configChanges, boolean dontReport) {
            int seq = getLifecycleSeq();
            if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
                    + " operation received seq: " + seq);
            sendMessage(
                    finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
                    token,
                    (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
                    configChanges,
                    seq);
        }
        
            SomeArgs args = (SomeArgs) msg.obj;
            handlePauseActivity((IBinder) args.arg1, false,
                (args.argi1 & USER_LEAVING) != 0, args.argi2,
                (args.argi1 & DONT_REPORT) != 0, args.argi3);
                maybeSnapshot();
 private void handlePauseActivity(IBinder token, boolean finished,
            boolean userLeaving, int configChanges, boolean dontReport, int seq) {
        ActivityClientRecord r = mActivities.get(token);
        if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
        if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
            return;
        }
        if (r != null) {
            //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
            //1
            if (userLeaving) {
                performUserLeavingActivity(r);
            }

            r.activity.mConfigChangeFlags |= configChanges;
            //2
            performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");

            //3 Make sure any pending writes are now committed.
            if (r.isPreHoneycomb()) {
                QueuedWork.waitToFinish();
            }

            // 4 Tell the activity manager we have paused.
            if (!dontReport) {
                try {
                    ActivityManager.getService().activityPaused(token);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }
            }
            mSomeActivitiesChanged = true;
        }
    }

注释1处调用成员函数performUserLeavingActivity向Launcher发送一个用户离开事件通知。注释2调用performPauseActivity使Launcher发送一个进入pause状态的通知即调用成员函数onPause。注释3处调用QueuedWork类的静态成员函数waitToFinish等待前面的一些数据写入操作。注释4dontReport为false,因此会调用AMS的activityPaused方法通知AMS Launcher已经进入Paused状态

   @Override
    public final void activityPaused(IBinder token) {
        final long origId = Binder.clearCallingIdentity();
        synchronized(this) {
            ActivityStack stack = ActivityRecord.getStackLocked(token);
            if (stack != null) {
                stack.activityPausedLocked(token, false);
            }
        }
        Binder.restoreCallingIdentity(origId);
    }
final void activityPausedLocked(IBinder token, boolean timeout) {
    final ActivityRecord r = isInStackLocked(token);
        if (r != null) {
            mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
            if (mPausingActivity == r) {
                if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
                        + (timeout ? " (due to timeout)" : " (pause complete)"));
                mService.mWindowManager.deferSurfaceLayout();
                try {
                    completePauseLocked(true /* resumeNext */, null /* resumingActivity */);
                } finally {
                    mService.mWindowManager.continueSurfaceLayout();
                }
                return;
            }
}
private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
    if (resumeNext) {
            final ActivityStack topStack = mStackSupervisor.getFocusedStack();
            //1
            if (!mService.isSleepingOrShuttingDownLocked()) {
                mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
            } else {
                mStackSupervisor.checkReadyForSleepLocked();
                ActivityRecord top = topStack.topRunningActivityLocked();
                if (top == null || (prev != null && top != prev)) {
                    // If there are no more activities available to run, do resume anyway to start
                    // something. Also if the top activity on the stack is not the just paused
                    // activity, we need to go ahead and resume it to ensure we complete an
                    // in-flight app switch.
                    mStackSupervisor.resumeFocusedStackTopActivityLocked();
                }
            }
        }
}

注释1处检查系统是不是正在进入睡眠或者关闭状态,如果不是就会调用resumeFocusedStackTopActivityLocked

boolean resumeFocusedStackTopActivityLocked(
            ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
        if (targetStack != null && isFocusedStack(targetStack)) {
            return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
        }
        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;
    }
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
 final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);

    if (next.app != null && next.app.thread != null) {
        ...
    }else {
            // Whoops, need to restart this activity!
            if (!next.hasBeenLaunched) {
                next.hasBeenLaunched = true;
            } else {
                if (SHOW_APP_STARTING_PREVIEW) {
                    next.showStartingWindow(null /* prev */, false /* newTask */,
                            false /* taskSwich */);
                }
                if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
            }
            if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
            mStackSupervisor.startSpecificActivityLocked(next, true, true);
        }
}

由于 topRunningActivityLocked获取的是位于Activity堆栈顶端的,即将启动的activity的一个ActivityRecord对象。由于对应Activity还没有启动,因此app为null

    void startSpecificActivityLocked(ActivityRecord r,
            boolean andResume, boolean checkConfig) {
        // 1 Is this activity's application already running?
        ProcessRecord app = mService.getProcessRecordLocked(r.processName,
                r.info.applicationInfo.uid, true);

        r.getStack().setLaunchTime(r);
       // 2
        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);
    }

在AMS中每个Activity组件都有一个用户ID和一个进程名称;其中用户ID是在安装该Activity时有PMS分配的,而进程名称则是由该Activity的android:process属性决定。AMS在启动一个Activity时,首先会用它的用户ID和进程名称来检查系统中是否存在一个对应的应用程序进程。如果存在,就会直接通知这个应用程序将该Activity启动起来,否则,就会先以这个用户ID和进程名称创建一个应用程序进程,然后再通知这个应用程序进程将该Activity启动起来。

注释1就是获取应用程序进程是否已经存在,如果存在就会直接调用realStartActivityLocked来启动该Activity,否则就会调用startProcessLocked为该Activity创建一个应用程序进程,然后将它启动起来。由于是第一次启动应用程序,这时候不可能存在所需的应用程序进程,因此会调用startProcessLocked来创建一个应用程序进程。

    final ProcessRecord startProcessLocked(String processName,
            ApplicationInfo info, boolean knownToBeDead, int intentFlags,
            String hostingType, ComponentName hostingName, boolean allowWhileBooting,
            boolean isolated, boolean keepIfLarge) {
        return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType,
                hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
                null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
                null /* crashHandler */);
    }
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
            boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
            boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
            String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
               
            if (hostingType.equals("webview_service")) {
                startResult = startWebView(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, null, entryPointArgs);
            } else {
                startResult = Process.start(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, invokeWith, entryPointArgs);
            }   
                
            }

在函数中调用了Process类的静态成员函数start来启动一个新的应用程序进程,该函数指定该进程的入口函数为android.app.ActivityThread类的静态成员函数main,接下来就开始一个新的应用程序进程启动。

frameworks/base/core/java/android/app/ActivityThread.java

public static void main(String[] args) {
     // Make sure TrustedCertificateStore looks in the right place for CA certificates
        final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
        TrustedCertificateStore.setDefaultUserDirectory(configDir);

        Process.setArgV0("<pre-initialized>");

        Looper.prepareMainLooper();

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

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

该函数主要做了两件事情:

  1. 在进程中创建一个ActivityThread对象,并调用它的成员函数attach向AMS发送一个启动完成通知。
  2. 调用Looper.prepareMainLooper()创建一个消息循环,并且在向AMS发送启动完成通知后,使得当前进程进入消息循环中。
private void attach(boolean system) {
        sCurrentActivityThread = this;
        mSystemThread = system;
        if (!system) {
            ViewRootImpl.addFirstDrawHandler(new Runnable() {
                @Override
                public void run() {
                    ensureJitEnabled();
                }
            });
            android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
                                                    UserHandle.myUserId());
            RuntimeInit.setApplicationObject(mAppThread.asBinder());
            final IActivityManager mgr = ActivityManager.getService();
            try {
                mgr.attachApplication(mAppThread);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        } 
    }

在该函数中获取AMS对象,然后调用它的成员函数attachApplication将创建的ApplicationThread对象传递给AMS

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

    @Override
    public final void attachApplication(IApplicationThread thread) {
        synchronized (this) {
            int callingPid = Binder.getCallingPid();
            final long origId = Binder.clearCallingIdentity();
            attachApplicationLocked(thread, callingPid);
            Binder.restoreCallingIdentity(origId);
        }
    }

    private final boolean attachApplicationLocked(IApplicationThread thread,
            int pid) {
        ProcessRecord app;
         // Find the application record that is being attached...  either via
        // the pid if we are running in multiple processes, or just pull the
        // next app record if we are emulating process with anonymous threads.
        //1
        long startTime = SystemClock.uptimeMillis();
        if (pid != MY_PID && pid >= 0) {
            synchronized (mPidsSelfLocked) {
                app = mPidsSelfLocked.get(pid);
            }
        } else {
            app = null;
        }
        ...
        
        app.makeActive(thread, mProcessStats);
        app.curAdj = app.setAdj = app.verifiedAdj = ProcessList.INVALID_ADJ;
        app.curSchedGroup = app.setSchedGroup = ProcessList.SCHED_GROUP_DEFAULT;
        app.forcingToImportant = null;
        updateProcessForegroundLocked(app, false, false);
        app.hasShownUi = false;
        app.debugging = false;
        app.cached = false;
        app.killedByAm = false;
        app.killed = false;
        app.unlocked = StorageManager.isUserKeyUnlocked(app.userId);
        mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
        boolean normalMode = mProcessesReady ||isAllowedWhileBooting(app.info);
        ...
        
       // 2 See if the top visible activity is waiting to run in this process...
        if (normalMode) {
            try {
                if (mStackSupervisor.attachApplicationLocked(app)) {
                    didSomething = true;
                }
            } catch (Exception e) {
                Slog.wtf(TAG, "Exception thrown launching activities in " + app, e);
                badApp = true;
            }
        } 
        
        ...
        
    }

参数pid指向了前面创建的应用程序进程PID,AMS以这个PID为关键字将ProcessRecord对象保存在成员变量mPidsSelfLocked中。注释1处通过pid将这个ProcessRecord对象取回来,并保存在变量app中。 由于normalMode未true注释2处调用attachApplicationLocked

frameworks/base/services/java/com/android/server/am/ActivityStackSupervisor.java

boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
        final String processName = app.processName;
        boolean didSomething = false;
        for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
            ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
            for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
                final ActivityStack stack = stacks.get(stackNdx);
                if (!isFocusedStack(stack)) {
                    continue;
                }
                ActivityRecord hr = stack.topRunningActivityLocked();
                if (hr != null) {
                    if (hr.app == null && app.uid == hr.info.applicationInfo.uid
                            && processName.equals(hr.processName)) {
                        try {
                            if (realStartActivityLocked(hr, app, true, true)) {
                                didSomething = true;
                            }
                        } catch (RemoteException e) {
                            Slog.w(TAG, "Exception in new application when starting activity "
                                  + hr.intent.getComponent().flattenToShortString(), e);
                            throw e;
                        }
                    }
                }
            }
        }
        if (!didSomething) {
            ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
        }
        return didSomething;
    }

由于目标Activity还没有和应用进程绑定hr.app为null,因此会调用realStartActivityLocked方法

final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
            boolean andResume, boolean checkConfig){
            ...
            
            r.app = app;
            app.waitingToKill = null;
            r.launchCount++;
            r.lastLaunchTime = SystemClock.uptimeMillis();
            //1
            app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
            System.identityHashCode(r), r.info,
            // TODO: Have this take the merged configuration instead of separate global and
            // override configs.
            mergedConfiguration.getGlobalConfiguration(),
            mergedConfiguration.getOverrideConfiguration(), r.compat,
            r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
            r.persistentState, results, newIntents, !andResume,
            mService.isNextTransitionForward(), profilerInfo);
            
            ...
    }

注释1处会调用app.thread.scheduleLaunchActivity来通知前面创建的的应用程序进程参数r所描述的一个Activity。

/frameworks/base/core/java/android/app/ActivityThread.java

    @Override
    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<ResultInfo> pendingResults, List<ReferrerIntent> 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;

            r.overrideConfig = overrideConfig;
            updatePendingConfiguration(curConfig);

            sendMessage(H.LAUNCH_ACTIVITY, r);
        }
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);

            if (!r.activity.mFinished && r.startsNotResumed) {
                // The activity manager actually wants this one to start out paused, because it
                // needs to be visible but isn't in the foreground. We accomplish this by going
                // through the normal startup (because activities expect to go through onResume()
                // the first time they run, before their window is displayed), and then pausing it.
                // However, in this case we do -not- need to do the full pause cycle (of freezing
                // and such) because the activity manager assumes it can just retain the current
                // state it has.
                performPauseActivityIfNeeded(r, reason);

                // We need to keep around the original state, in case we need to be created again.
                // But we only do this for pre-Honeycomb apps, which always save their state when
                // pausing, so we can not have them save their state when restarting from a paused
                // state. For HC and later, we want to (and can) let the state be saved as the
                // normal part of stopping the activity.
                if (r.isPreHoneycomb()) {
                    r.state = oldState;
                }
            }
        }
}
      

在该方法中会调用performLaunchActivity和handleResumeActivity方法。

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
 ContextImpl appContext = createBaseContextForActivity(r);
        Activity activity = null;
        try {
            java.lang.ClassLoader cl = appContext.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);
            }
        }
        
        Application app = r.packageInfo.makeApplication(false, mInstrumentation);
        
        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);
        
        if (r.isPersistable()) {
            mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
        } else {
            mInstrumentation.callActivityOnCreate(activity, r.state);
        }
        
        if (!r.activity.mFinished) {
            activity.performStart();
            r.stopped = false;
        }
   
}

方法中会调用Actvity的onCreate和onStart方法,Activity正是启动了。

参考资料
《Android系统源码情景分析》罗升阳

Android深入四大组件(六)Android8.0 根Activity启动过程(前篇) 当我开始阅读Android 8.0源码时发现应用程序(根Activity)启动过程照Android 7.0有了一些变化,因此又写下了本篇文章,本篇文章照此前的文章不仅流程发生变化,而且增加了一些分析,算是升级版本。由于篇幅较长,Android8.0 根Activity启动过程仍旧分为前篇和后篇来进行讲解。 阅读详情

相关推荐

Android深入四大组件(七)Android8.0 根Activity启动过程(后篇)

当我开始阅读Android 8.0源码时发现应用程序(根Activity)启动过程照Android 7.0有了一些变化,因此又写下了本篇文章,本篇文章照此前的文章不仅流程发生变化,而且增加了一些分析,算是升级版本。由于篇幅较长,Android8.0 根Activity启动过程仍旧分为前篇和后篇来进行讲解。

Android进阶三部曲 - 刘望舒 2291

解析Activity的启动过程

Activity启动过程 Activity 的启动过程分为两种, 一种是根 Activity 的启动过程,另一种是普通Activity(子Activity)的启动过程。 根Activity:以快捷图标的形式显示在Launcher中,它的启动过程代表了一个Android应用程序的启动过程。 子Activity:由根Activity或其他子Activity启动,他们有可能与启动他们的Activity运行在同一个进程中,也可能运行在不同的进程中。 ...

lixiong0713的博客 2万+

ROS 2实战:用MoveIt Task Constructor让机械臂完成抓取任务(附避坑指南)

本文详细介绍了如何在ROS 2环境中使用MoveIt Task Constructor框架,为Franka Panda机械臂构建一个完整的拾取放置任务流水线。通过分解任务阶段、配置规划器并处理实际部署中的常见问题,帮助开发者从基础运动规划升级到可靠的多步骤任务编排,并附有核心避坑指南。

vvv99的博客 514

Android 根Activity的启动过程

四大组件是应用开发最长接触的,包括Activity、Service、BroadcastReceiver、ContentProvider。本文不会介绍四大组件的含义及如何使用,而是深入的介绍他们的工作过程。 本文是基于Android 8.0 源码,所以部分函数,以及一些中间类可能在低版本的源码中没有。 1. 根Activity的启动过程 Activity的启动有两种,一种是根Activity的启动过...

王小_的博客 613

Comsol 超声空化气泡仿真

当液体中受到高能量的超声波作用时,液体中的压力会发生剧烈变化,形成局部的低压区域。当液体中的压力降低到饱和蒸汽压以下时,就会发生空化现象,即液体中形成微小气泡。这种空化常用于一些工业应用,如清洗、乳化、混合等。这种空化常用于一些医学和生物学应用,如碎石术、肿瘤治疗、细胞破碎等。超声空化的应用非常广泛。在医学领域,超声空化广泛应用于诊断和治疗,如超声造影、超声刀、超声碎石术等。在工业领域,超声空化可用于清洗、乳化、混合等工艺。此外,超声空化还在食品加工、化学合成、环境保护等领域有着重要应用。

声学黑洞仿真工作室 2571

华为OD机试真题 新系统2026-04-01 C语言 实现【勇攀数字高峰】

请你在给定的数字地形图中寻找登山路径,数字代表当前位置的海拔高度,要求从最低海拔出发,不断攀登,最终到达最高山峰。你需要寻找所有满足条件的登山路径。地图已经保证最低海拔和最高山峰都只有一个。

MISAYAONE的博客 302

根Activity的启动过程

《安卓进阶解密》读书笔记 这里分析的源码基于android-8.0.0_r4. 「根Activity的启动过程」和「Activity的启动_5.0」笔记记录的,有大部分过程是一样的. 概述 根Activity启动过程中设计到的进程及其交互 时序图如下 这里将根Activity的启动划分为四部分 Launcher请求AMS过程 AMS判断根Activity所需的应用程序进程是否已经存在并启动,若不存在,则请求Zygote进程创建应用程序进程,详情查看《应用程序进程启动过程》的笔记。应用程序进程启动后.

Giagor的博客 1257

AI智能体丨使用Coze一键生成爆款书单视频,3个月内获赞131.1w!(附保姆级教程)

这篇教程详细介绍了如何使用扣子(Coze)平台的AI智能工作流批量制作爆款书单视频。通过自动化流程,只需1-2分钟即可生成45-50秒的书单视频,效率提升近百倍。工作流包含文案生成、音频合成、图片素材获取、视频剪辑等全流程,支持随机生成8本书单、自动提取金句、智能配音等功能。教程提供了完整的操作步骤,包括剪映草稿配置、特效添加等细节,并附体验链接和示例效果。最终生成的视频包含书单展示、金句字幕、背景音乐等元素,适合书单号运营者快速批量产出内容。

李同学Lino的博客 2363

Android:Context完全解析,你所不知道的Context的各种细节

Context的各种细节

微不足道的博客 2182

Oracle EBS的帐套由“4C”构成,而华为MetaERP将其发展为“6C”

华为MetaERP从OracleEBS的"4C"架构升级为"6C",实现核算理念革新。核心突破在于将会计科目、核算维度和核算单元解耦,形成"科目表+维度库"的灵活架构。新架构优势显著:1)科目回归会计本质,管理维度独立扩展;2)支持无限维度的精细化核算;3)实现数据权限隔离和内部交易自动化;4)采用元数据驱动设计,为AI应用奠定基础。这一平台化架构解决了大型企业在全球化、多元化发展中的核算难题,相比传统架构具有更强的灵活性和扩展性。

金牌架构师 207

【QNX+Android虚拟化方案】12 - 设备虚拟化 之 Passthrough透传、Vdev Trap、HAB Socket 原理解析

【QNX+Android虚拟化方案】12 - 设备虚拟化 之 Passthrough透传、Vdev Trap、HAB Socket 原理解析

|~~~热爱生活、努力学习的小伙汁~~~| 1394

Activity启动过程详解(Android 12源码分析)

activity启动流程

yangfengxiao的专栏 6万+

别再盯着那 100 条数据了,RAP OData 分页机制到底在保护什么

摘要: SAP BTP ABAP Environment 的 OData 服务默认采用分页保护机制,未指定 $top 时仅返回100条数据并附带下一页链接。这种设计旨在防止资源滥用,而非服务故障。客户端应遵循OData协议规范,直接使用服务端返回的 next link 进行分页请求,而非自行构造URL。当指定 $top 参数时,若数值超过5000仍会被强制分页。该机制在SAPUI5控件中被自动处理,但自定义客户端需主动实现分页逻辑。对于自定义实体(Custom Entity),开发者需在Query Prov

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 877

【Android关键流程】根Activity的启动过程

摘要:当用户点击Launcher应用图标时,会触发Launcher#startActivitySafely方法,通过继承链最终调用Activity#startActivityForResult。关键步骤包括:1)设置FLAG_ACTIVITY_NEW_TASK标志;2)调用Instrumentation#execStartActivity方法;3)获取ATMS代理对象并调用startActivity()方法。

weixin_42321153的博客 2473

手把手教你学 GPU KMD”相关内容的专栏简介与目录

本专栏系统讲解GPU内核模式驱动(KMD)开发技术,涵盖四大核心模块:1)GPU驱动架构基础,解析UMD/KMD协同机制;2)KMD核心功能,包括内存管理、命令提交、中断处理等;3)底层硬件交互,详解PCIe/DMA传输、多级页表等;4)实战进阶,结合Linux DRM框架代码分析,探讨GPU虚拟化与性能优化。面向具备OS和C语言基础的开发者,提供从理论到实践的完整学习路径,帮助深入理解图形/计算驱动栈的底层实现原理。

amy_mhd的博客 162

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

根Activity的启动过程 面试笔记

面试小结 高频面试 Activity的启动过程分为两种,一种是根Activity的启动过程,另一种是普通Activity的启动过程 分三个部分: 1、Launcher 请求AMS过程 2、AMS到ApplicationThread的调用过程 3、ActivityThread 启动 Activity //#################################分割线 ################################################# 1、Launcher 请求AMS过

Gsony的博客 357

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

根Activity启动过程中涉及的进程 面试 笔记

面试小结 根Activity启动过程中涉及的进程之间的关系 //#################################分割线 ################################################# 根Activity 启动过程中进程调用时序图

Gsony的博客 220

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Android系统源码阅读(2):根Activity组件的启动过程

Android系统源码阅读(2):根Activity组件的启动过程 该系列只记录阅读代码时遇到的问题和心得体会,具体代码讲解可以参考老罗的《Android系统源代码情景分析》,我就不班门弄斧了。我编译的AOSP版本:6.0.1_r50。 Android系统源码阅读2根Activity组件的启动过程 代码摘抄 两个版本的Launcher Android的Singleton模式的实现 Activit

天天吃吃 1419

Android根Activity的启动过程

本文是《Android进阶解密》一书的读书笔记。所以,大家可以直接去买书看看。 现在开始记录根Activity的启动过程。该过程很复杂,讲述的是从手机开机进入桌面之后,点击屏幕上App的应用图标开始,直到App启动后显示的第一个应用程序Activity的过程。可以分为三个部分,分别是Launcher请求AMS的过程,AMS到ApplicationThread的调用过程和ActivityThread启动Activity的过程。 一、Launcher请求AMS的过程 1. 点击应用图标会调用Launche

小小时光 497

Android 进阶解密:根 Activity 启动过程

一、根 Activity 启动过程 可以分为三个部分: ① Launcher 请求 AMS 过程 ② AMS 到 ApplicationThread 的调用过程 ③ ActivityThread 启动 Activity 二、Launcher 启动 AMS 过程 在桌面点击应用图标,会调用 Launcher#startActivitySafely() 方法 public boolean startA...

韭菜鲜肉大馄饨的专栏 808

根Activity组件启动过程

图例只描述了Activity组件在进程外的启动过程,即从Launcher点击图标启动MainActivity的过程。MainActivity的启动过程涉及到了三个进程。MainActivity组件、LauncherActivity组件和ActivityManagerService组件分别运行在不同的进程中。

沐唐风宋雨 388

根Activity启动过程中涉及的进程

在Activity启动过程中间会涉及4个进程 分别是Zygote进程、Launcher进程、AMS所在进程(SystemServer进程)、应用进程。他们关系如下 首先Launcher进程向AMS请求请求创建根Activity,AMS 判断根Activity 所需的应用进程是否存在并启动,如果不存在就会请求Zygote进程创建应用程序,应用程序进程启动后,AMS会请求创建应用程序进程并启动Activity .步驟2 采用的Socket通信 步骤1 和步骤4 采用的Binder通信 时序图可以总结如下.

u012845099的博客 312

进阶解密-四大组件的工作过程-根Activity的启动过程

根Activity的启动过程 Activity的启动过程分为两种,一种是根Activity的启动过程,另一种是普通Activity的启动过程。 根Activity的启动过程比较复杂,因此这里分为3个部分来讲,分别是Launcher请求AMS过程、AMS到ApplicationThread的调用过程和ActivityThread启动Activity。 1.Launcher请求AMS过程 2.AMS到...

qq_36142139的博客 220

Android深入四大组件(四)Android8.0 根Activity启动过程(一)

Android深入四大组件(四)Android8.0 根Activity启动过程(前篇) 前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读Android 8.0源码时发现应用程序(根Activity)启动过程照Android 7.0有了一些变化,因此又写下了本篇文章,本篇文章照此前的文章不仅流程发生变化,而且增加了一些分析,算是升级版本。由于篇幅较长,And

heng615975867的专栏 567
上一篇: 线程之间的协作
下一篇: 消息处理机制之消息循环
coderPlus
博客等级 码龄12年 1粉丝 24原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址