Android进阶解密1⃣️—Activity的启动流程

本文详细剖析了Android根Activity的启动过程,从Launcher请求AMS开始,经过AMS到ApplicationThread,再到ActivityThread启动Activity,涉及多个关键步骤和方法,如startActivitySafely()、execStartActivity()、realStartActivityLocked()等。
摘要由CSDN通过智能技术生成

Activity的启动分为根activity启动和普通activity启动,根activity的启动过程包括了普通activity的启动过程,本文只介绍根activity的启动;

根Activity启动的整理流程:

  1. Launcher进程请求SystemServer进程的AMS
  2. AMS请求用户进程的ApplicationThread
  3. ApplicationThread请求ActivityThread,ActivityThread启动Activity

步骤1 Launcher请求AMS

Launcher进程 ~> AMS

在Launcher桌面点击应用图标会调用Launcher的startActivitySafely()

1.1 Launcher.startActivitySafely()
    boolean startActivitySafely(View v, Intent intent, Object tag) {
   
        boolean success = false;
        try {
   
            success = startActivity(v, intent, tag);
        }
        return success;
    }

  boolean startActivity(View v, Intent intent, Object tag) {
   
        //  分析1
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        // 分析2
        startActivity(intent, opts.toBundle());
    }
  • 通过intent设置New Task的Flag,开启一个新的活动栈
  • 调用startActivity(),这个方法在Activity类中实现;
1.2 Activity.startActivityForResult():

startActivity()会调用startActivityForResult()

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

在Activity类中,将启动Activity的请求交给了Instrumentation类

1.3 Instrumentation.execStartActivity():
    public ActivityResult execStartActivity(
            Context who, IBinder contextThread, IBinder token, Activity target,
            Intent intent, int requestCode, Bundle options) {
   

           int result = ActivityManager.getService()
                .startActivityAsUser(whoThread, who.getBasePackageName(), intent,
                        intent.resolveTypeIfNeeded(who.getContentResolver()),
                        token, resultWho,
                        requestCode, 0, null, options, user.getIdentifier());

    }

通过 ActivityManager.getService()拿到iActivityManager对象,iActivityManager是AMS在用户进程的代理,所以真实调用的其实是AMS的startActivityAsUser();
至此Launcher到AMS的流程分析完毕

步骤2 AMS到ApplicationThread

AMS ~> 用户进程

2.1 AMS.startActivityAsUser():
    @Override
    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) {
   
            //  分析1
            userId = mUserController.handleIncomingUser(Binder.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值