activity view显示流程

本文详细探讨了Android应用Activity从handleLaunchActivity开始的显示流程,包括PhoneWindow的setContentView、ViewRootImpl的setView和WindowManager的添加窗口过程。通过分析关键步骤,如添加 DecorView、requestLayout以及在WMS中的窗口类型管理,揭示了UI绘制和事件处理的核心机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先来个序列图:
在这里插入图片描述

1.handleLaunchActivity

    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
   
   
        // 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
        if (!ThreadedRenderer.sRendererDisabled) {
   
   
            GraphicsEnvironment.earlyInitEGL();
        }
        WindowManagerGlobal.initialize();

		//这个函数调用就是onCreate和onStart生命周期的两个重要调用者
        Activity a = **performLaunchActivity**(r, customIntent);

        if (a != null) {
   
   
            r.createdConfig = new Configuration(mConfiguration);
            reportSizeConfigurations(r);
            Bundle oldState = r.state;
       //这个函数调用是为了调用onResume()
            **handleResumeActivity**(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);

我们一般写APP,会重载Activity的onCreate函数。在这个函数里面,一般情况下会调用setContentView(R.layout.xxxx)来设置我们的布局文件,所以从这个函数开始跟踪其逻辑流程,以便从这个角度来了解WMS的基本工作流程

2.Activity setContentView

    public void setContentView(@LayoutRes int layoutResID) {
   
   
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

这里的getWindow()就是获取PhoneWindow的实例,这个实例是在attach的时候new的,如图:
在这里插入图片描述

3.PhoneWindow的setContentView

    @Override
    public void setContentView(int layoutResID) {
   
   
        // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
        // decor, when theme attributes and the like are crystalized. Do not check the feature
        // before this happens.
        if (mContentParent == null) {
   
   
        //主要生成DecorView实例和ViewGroup实例,具体的生成过程可以自行研究代码
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
   
   
            mContentParent.removeAllViews();
        }

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
   
   
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bruk_spp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值