2024年最全Android Activity 启动流程分析_android activity工作流程(1),2024最新京东Android面试题目

架构师筑基包括哪些内容

我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!



这份资料就包含了所有Android初级架构师所需的所有知识!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

        true /*validateIncomingUser*/);

}



#ActivityTaskManagerService.java
private int startActivityAsUser(IApplicationThread caller, String callingPackage,
@Nullable String callingFeatureId, Intent intent, String resolvedType,
IBinder resultTo, String resultWho, int requestCode, int startFlags,
ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) {
//检查调用者权限
userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser,
Binder.getCallingPid(), Binder.getCallingUid(), “startActivityAsUser”);
// TODO: Switch to user app stacks here.
return getActivityStartController().obtainStarter(intent, “startActivityAsUser”)
.setCaller(caller)
.setCallingPackage(callingPackage)
.setCallingFeatureId(callingFeatureId)
.setResolvedType(resolvedType)
.setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(bOptions)
.setUserId(userId)
.execute();
}


上面代码最终调用到了 `startActivityAsUser` 方法,在内部将所有点的参数都交给了 ActivityStarter ,该类包含了启动的所有逻辑,比如 Intent 解析以及任务栈等。


接着调用了`obtainStarter` ,该方法通过工厂模式创建了 ActivityStarter 对象,如下所示:



#ActivityStarter.java
static class DefaultFactory implements Factory {
/**
* ActivitySatrter 最大数量
*/
private final int MAX_STARTER_COUNT = 3;

//同步池
private SynchronizedPool mStarterPool =
new SynchronizedPool<>(MAX_STARTER_COUNT);

DefaultFactory(ActivityTaskManagerService service,
        ActivityTaskSupervisor supervisor, ActivityStartInterceptor interceptor) {
    mService = service;
    mSupervisor = supervisor;
    mInterceptor = interceptor;
}

@Override
public void setController(ActivityStartController controller) {
    mController = controller;
}

@Override
public ActivityStarter obtain() {
    //从同步池中获取 ActivityStarter 对象
    ActivityStarter starter = mStarterPool.acquire();
    if (starter == null) {
        if (mService.mRootWindowContainer == null) {
            throw new IllegalStateException("Too early to start activity.");
        }
        starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);
    }
    return starter;
}

@Override
public void recycle(ActivityStarter starter) {
    starter.reset(true /* clearRequest*/);
    mStarterPool.release(starter);
}

}


可以看到,默认的工厂在提供了一个容量为 3 的同步缓存池来缓存 ActivityStarter 对象,该对象创建完成之后,该对象创建完成之后,AMTS 就会将接下来启动 Activity 的操作交给 ActivityStarter 来完成。



#ActivityStarter.java
//根据前面传入的参数解析一下必要的信息,并开始启动 Activity
int execute() {
try {
int res;
synchronized (mService.mGlobalLock) {

res = executeRequest(mRequest);//开始执行请求

return getExternalResult(res);
}
} finally {
onExecutionComplete();
}
}



#ActivityStarter.java
private int executeRequest(Request request) {

//检测Activity启动的权限
boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,requestCode, callingPid, callingUid, callingPackage, callingFeatureId,request.ignoreTargetSecurity, inTask != null, callerApp, resultRecord, resultRootTask);
abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
callingPid, resolvedType, aInfo.applicationInfo);
abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,
callingPackage);

final ActivityRecord r = new ActivityRecord.Builder(mService)
        .setCaller(callerApp)
        .setLaunchedFromPid(callingPid)
        .setLaunchedFromUid(callingUid)
        .setLaunchedFromPackage(callingPackage)
        .setLaunchedFromFeature(callingFeatureId)
        .setIntent(intent)
        .setResolvedType(resolvedType)
        .setActivityInfo(aInfo)
        .setConfiguration(mService.getGlobalConfiguration())
        .setResultTo(resultRecord)
        .setResultWho(resultWho)
        .setRequestCode(requestCode)
        .setComponentSpecified(request.componentSpecified)
        .setRootVoiceInteraction(voiceSession != null)
        .setActivityOptions(checkedOptions)
        .setSourceRecord(sourceRecord)
        .build();

mLastStartActivityRecord = r;

mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
        request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask,
        restrictedBgActivity, intentGrants);

return mLastStartActivityResult;

}


上面代码中会进行一些校验和判断权限,包括进程检查,intent检查,权限检查等,后面就会创建 `ActivityRecord` ,每个 Activity 都会对应一个 `ActivityRecord` 对象,接着就会调用 `startActivityUnchecked` 方法对要启动的 Activity 做任务栈管理。



#ActivityStarter.java
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, Task inTask,
boolean restrictedBgActivity, NeededUriGrants intentGrants) {
int result = START_CANCELED;
final Task startedActivityRootTask;
try {
mService.deferWindowLayout();
Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, “startActivityInner”);
result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options, inTask, restrictedBgActivity, intentGrants);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
startedActivityRootTask = handleStartResult(r, result);
mService.continueWindowLayout();
}

postStartActivityProcessing(r, result, startedActivityRootTask);

return result;

}


在大多数初步检查已经完成的情况下开始进行下一步确认拥有必要的权限。 上面的核心方法就是 `startActivityInner()` 用来检查启动所必须要有的权限



#ActivityStarter.java
int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, Task inTask,
boolean restrictedBgActivity, NeededUriGrants intentGrants) {
//设置初始化状态
setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
voiceInteractor, restrictedBgActivity);
//判断启动模式,并且在 mLaunchFlags 上追加对应标记
computeLaunchingTaskFlags();
//设置 Activity 的栈
computeSourceRootTask();
//设置 LaunchFlags 到 intent 上
mIntent.setFlags(mLaunchFlags);

//决定是否应将新活动插入现有任务中。返回null, 如果不是则应将新活动添加到其中的任务进行活动记录
final Task reusedTask = getReusableTask();

......

//reusedTask 为 null 则计算是否存在可以使用的任务栈
final Task targetTask = reusedTask != null ? reusedTask : computeTargetTask();
//是否需要创建栈  
final boolean newTask = targetTask == null;
mTargetTask = targetTask;

computeLaunchParams(r, sourceRecord, targetTask);

//检查是否允许在给定任务或新任务上启动活动
int startResult = isAllowedToStart(r, newTask, targetTask);
if (startResult != START_SUCCESS) {
    return startResult;
}

final ActivityRecord targetTaskTop = newTask
        ? null : targetTask.getTopNonFinishingActivity();
if (targetTaskTop != null) {
    // Recycle the target task for this launch.
    startResult = recycleTask(targetTask, targetTaskTop, reusedTask, intentGrants);
    if (startResult != START_SUCCESS) {
        return startResult;
    }
} else {
    mAddingToTask = true;
}

//如果正在启动的活动与当前位于顶部的活动相同
//则需要检查它是否应该只启动一次
final Task topRootTask = mPreferredTaskDisplayArea.getFocusedRootTask();
if (topRootTask != null) {
    startResult = deliverToCurrentTopIfNeeded(topRootTask, intentGrants);
    if (startResult != START_SUCCESS) {
        return startResult;
    }
}

//复用或者创建新栈
if (mTargetRootTask == null) {
    mTargetRootTask = getLaunchRootTask(mStartActivity, mLaunchFlags, targetTask, mOptions);
}
if (newTask) {
  	//新建一个 Task
    final Task taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)
            ? mSourceRecord.getTask() : null;
    setNewTask(taskToAffiliate);
} else if (mAddingToTask) {
    //复用之前的 Task
    addOrReparentStartingActivity(targetTask, "adding to task");
}

	......
if (mDoResume) {
		// 调用 resumeFocusedTasksTopActivities方法
  mRootWindowContainer.resumeFocusedTasksTopActivities(
                mTargetRootTask, mStartActivity, mOptions, mTransientLaunch);
}
mRootWindowContainer.updateUserRootTask(mStartActivity.mUserId, mTargetRootTask);
.....
return START_SUCCESS;

}


在上面方法中,根据启动模式计算出 flag,然后在根据 flag 等条件判断要启动的 Activity 的 ActivityRecord 是需要新创建 Task 栈 还是加入到现有的 Task 栈。


在为 Activity 准备好 Task 栈之后,调用了 mRootWindowContainer.resumeFocuredTasksTopActivities 方法。



#RootWindowContainer.java
boolean resumeFocusedTasksTopActivities(
Task targetRootTask, ActivityRecord target, ActivityOptions targetOptions,
boolean deferPause) {
boolean result = false;
if (targetRootTask != null && (targetRootTask.isTopRootTaskInDisplayArea()
|| getTopDisplayFocusedRootTask() == targetRootTask)) {
result = targetRootTask.resumeTopActivityUncheckedLocked(target, targetOptions,deferPause);
}
return result;
}



#Task.java
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options,boolean deferPause) {
if (mInResumeTopActivity) {
// Don’t even start recursing.
return false;
}

boolean someActivityResumed = false;
try {
    // Protect against recursion.
    mInResumeTopActivity = true;
    ....
    someActivityResumed = resumeTopActivityInnerLocked(prev,options,deferPause);
} finally {
    mInResumeTopActivity = false;
}

return someActivityResumed;

}



#Task.java
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options,
boolean deferPause) {
if (!mAtmService.isBooting() && !mAtmService.isBooted()) {
// Not ready yet!
return false;
}

// 在当前 Task 栈中找到最上层正在运行的 Activity
// 如果这个 Activity 没有获取焦点,那这个 Activity 将会被重新启动
ActivityRecord next = topRunningActivity(true /* focusableOnly */);

if (next.attachedToProcess()) {
    ......
} else {
    ......
    //调用 ActivityTaskSupervisor.startSpecificActivity
    mTaskSupervisor.startSpecificActivity(next, true, true);
}

return true;

}



#ActivityTaskSupervisor.java
void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) {
// 获取目标进程
final WindowProcessController wpc =
mService.getProcessController(r.processName, r.info.applicationInfo.uid);

    boolean knownToBeDead = false;
    if (wpc != null && wpc.hasThread()) {
        try {
            //如果进程存在,启动 Activity 并返回
            realStartActivityLocked(r, wpc, andResume, checkConfig);
            return;
        } catch (RemoteException e) {

        }
        knownToBeDead = true;
    }
    r.notifyUnknownVisibilityLaunchedForKeyguardTransition();
    final boolean isTop = andResume && r.isTopRunningActivity();
    // 进程不存在,则创建进程,并启动 Activity
    mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
}

如果进程不存在,则会创建进程,如果进程存在,则执行此方法



boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
boolean andResume, boolean checkConfig) throws RemoteException {

// 创建启动 Activity 的事务
// proc.getThread() 获取的是一个 IApplicationThread 对象
final ClientTransaction clientTransaction = ClientTransaction.obtain(
          proc.getThread(), r.appToken);

final boolean isTransitionForward = r.isTransitionForward();
// 为事务设置 Callback LaunchActivityItem,在客户端时会被调用
clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent), 											System.identityHashCode(r), r.info,
        mergedConfiguration.getGlobalConfiguration(),
        mergedConfiguration.getOverrideConfiguration(), r.compat,
        r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),
        r.getSavedState(), r.getPersistentSavedState(), results, newIntents,
        r.takeOptions(), isTransitionForward,
        proc.createProfilerInfoIfNeeded(), r.assistToken, 					
        activityClientController,
        r.createFixedRotationAdjustmentsIfNeeded(), r.shareableActivityToken,
        r.getLaunchedFromBubble()));

 // 生命周期对象
 final ActivityLifecycleItem lifecycleItem;
 if (andResume) {
     lifecycleItem = ResumeActivityItem.obtain(isTransitionForward);
 } else {
     lifecycleItem = PauseActivityItem.obtain();
 }
 // 设置生命周期请求
 clientTransaction.setLifecycleStateRequest(lifecycleItem);

 // 执行事务
 mService.getLifecycleManager().scheduleTransaction(clientTransaction);

return true;

}


上面代码的核心就是创建事务实例,然后来启动 Activity 。


ClientTransaction 是一个容器,里面包含了一些列的消息,这些消息会被发送到客户端,这些消息包括了一系列的回调和一个最终的生命周期状态。


ActivityLifecycleItem 用来请求 Activity 应该到达那个生命周期。


ClientLifecycleManager 用来执行事务


接着上面的代码往下走,就到了 `scheduleTransaction()`:



void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
// AIDL 接口,在客户端被实现,也就是 app 中
final IApplicationThread client = transaction.getClient();
//执行事务
transaction.schedule();
if (!(client instanceof Binder)) {
transaction.recycle();
}
}



public void schedule() throws RemoteException {
mClient.scheduleTransaction(this);
}


mClient 就是 IApplicationThread 的实例,这里是一个 IPC 调用,会直接调用到 App 进程中,并传入了 this,也就是 ClientTransaction 对象。


IApplicationThread 是 ApplicationThread 所实现的,**他是 ActivityThread 的内部类**:



private class ApplicationThread extends IApplicationThread.Stub {
@Override
public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
ActivityThread.this.scheduleTransaction(transaction);
}
}


我们用一张图来描述一下上面的流程


![](https://img-blog.csdnimg.cn/img_convert/855fffad0eb1efb1ad9cbee5185daa7b.png)


在上面代码中检查 intent 以及各种权限,并且会获取启动模式,设置启动 Activity 的Task,最后判断 Activity 所在的进程是否存活,如果不存在则创建,如果存在则会通过 IPC 回调到 ApplicationThread 中去。


#### 三、ActivityThread


通过上面,我们知道了启动 Activity 最终有回调到 ApplicationThread,而它又是 ActivityThread 的子类。所以 上面代码最终调用到了 ActivityThread 的父类 ClientTransactionHandler 中:



void scheduleTransaction(ClientTransaction transaction) {
transaction.preExecute(this);//处理
//发送消息
sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
}



//创建的时候传入了 ActivityThread 实例
private final TransactionExecutor mTransactionExecutor = new TransactionExecutor(this);

#ActivityThread$H
case EXECUTE_TRANSACTION:
final ClientTransaction transaction = (ClientTransaction) msg.obj;
mTransactionExecutor.execute(transaction);
if (isSystem()) {
transaction.recycle();
}
break;


上面代码中通过 `TransactionExecutor` 来执行事务:



public void execute(ClientTransaction transaction) {
//执行回调
executeCallbacks(transaction);
//处理生命周期状态
executeLifecycleState(transaction);
mPendingActions.clear();
if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + “End resolving transaction”);
}

//处理生命周期状态
private void executeLifecycleState(ClientTransaction transaction) {
final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest();
if (lifecycleItem == null) {
// No lifecycle request, return early.
return;
}

final IBinder token = transaction.getActivityToken();
final ActivityClientRecord r = mTransactionHandler.getActivityClient(token);
if (DEBUG_RESOLVER) {
    Slog.d(TAG, tId(transaction) + "Resolving lifecycle state: "
            + lifecycleItem + " for activity: "
            + getShortActivityName(token, mTransactionHandler));
}

if (r == null) {
    // Ignore requests for non-existent client records for now.
    return;
}

// 切换到对应的生命周期
cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */, transaction);

// Execute the final transition with proper parameters.
lifecycleItem.execute(mTransactionHandler, token, mPendingActions);
lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);

}

private void cycleToPath(ActivityClientRecord r, int finish, boolean excludeLastState,
ClientTransaction transaction) {
final int start = r.getLifecycleState();

final IntArray path = mHelper.getLifecyclePath(start, finish, excludeLastState);
performLifecycleSequence(r, path, transaction);

}

//执行生命周期状态
private void performLifecycleSequence(ActivityClientRecord r, IntArray path,
ClientTransaction transaction) {
final int size = path.size();
for (int i = 0, state; i < size; i++) {
state = path.get(i);
switch (state) {
case ON_CREATE:
mTransactionHandler.handleLaunchActivity(r, mPendingActions,
null /* customIntent /);
break;
case ON_START:
mTransactionHandler.handleStartActivity(r, mPendingActions,
null /
activityOptions /);
break;
case ON_RESUME:
mTransactionHandler.handleResumeActivity(r, false /
finalStateRequest /,
r.isForward, “LIFECYCLER_RESUME_ACTIVITY”);
break;
case ON_PAUSE:
mTransactionHandler.handlePauseActivity(r, false /
finished /,
false /
userLeaving /, 0 / configChanges /, mPendingActions,
“LIFECYCLER_PAUSE_ACTIVITY”);
break;
case ON_STOP:
mTransactionHandler.handleStopActivity(r, 0 /
configChanges /,
mPendingActions, false /
finalStateRequest /,
“LIFECYCLER_STOP_ACTIVITY”);
break;
case ON_DESTROY:
mTransactionHandler.handleDestroyActivity(r, false /
finishing /,
0 /
configChanges /, false / getNonConfigInstance /,
“performLifecycleSequence. cycling to:” + path.get(size - 1));
break;
case ON_RESTART:
mTransactionHandler.performRestartActivity(r, false /
start */);

最后

分享一份工作1到5年以上的Android程序员架构进阶学习路线体系,希望能对那些还在从事Android开发却还不知道如何去提升自己的,还处于迷茫的朋友!

  • 阿里P7级Android架构师技术脑图;查漏补缺,体系化深入学习提升

  • **全套体系化高级架构视频;**七大主流技术模块,视频+源码+笔记

有任何问题,欢迎广大网友一起来交流

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

       case ON_RESTART:
            mTransactionHandler.performRestartActivity(r, false /* start */);

最后

分享一份工作1到5年以上的Android程序员架构进阶学习路线体系,希望能对那些还在从事Android开发却还不知道如何去提升自己的,还处于迷茫的朋友!

  • 阿里P7级Android架构师技术脑图;查漏补缺,体系化深入学习提升

    [外链图片转存中…(img-KNIPRiIF-1715838243575)]

  • **全套体系化高级架构视频;**七大主流技术模块,视频+源码+笔记

[外链图片转存中…(img-P9m2LQ8n-1715838243575)]

有任何问题,欢迎广大网友一起来交流

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值