Activity VS View

作者: ZDCrazy | 来源:发表于2018-05-02 11:23 被阅读62次

  最近有幸作为一面官,有面试了一些新人关于 Android 的基础知识(自己才工作了一年..),或许是处于一点自私的角度,将自己刚毕业时关于 Android 的一点困惑也问了这些新人。果然困惑的人都有同样的困惑,面试他们就像看到了自己刚毕业时候的影子一样。

  本篇主要说明一下 Activity 生命周期和 View 的关联关系,即View 测量(onMeasure())、布局(onLayout())、绘制(onDraw()),以及 View 的一些回调事件比如 onAttachedToWindow()、onDetachedFromWindow()、onFinishInflate()对应Activity的生命周期关系。好多新人对这一块都比较模糊。

  测试代码如下:

/**
 *  MainActivity 布局包含 CustomLayout
 */
public class PtrDemoHomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        log("activity onCreate");
        setContentView(R.layout.activity_main);
       // pushFragmentToBackStack(PtrDemoHomeFragment.class, null);
    }

    @Override
    protected void onStart() {
        super.onStart();
        log("activity onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        log("activity onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        log("activity onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        log("activity onDestroy");
    }
}

  MainActivity布局文件中,只包含一个简单的自定义的CustomLayout,CustomLayout代码如下:

/**
 * Created by zhangdan on 2018/5/5.
 * <p>
 * comments:
 */
public class CustomLayout extends LinearLayout {

    public CustomLayout(Context context) {
        super(context);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        log("view onAttachedToWindow");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        log("view onFinishInflate");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        log("view onMeasure");
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        log("view onLayout");
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        log("on DetachedFromWindow");
    }
}

运行结果如下:


result

下面从代码角度依次分析一下上面执行顺序的原因。

Activity.onCreate() > View.onFinishInflate() :

当需要启动一个Activity时候,最后都会由AMS调用到Activity Thread的performLaunchActivity(),在该方法中,首先会回调到Activity的onCreate()方法,平时我们都是在onCreate中setContentView()去设置界面布局,那么setContentView()和View.onFinishInflate()有什么关系呢,代码注释如下:

Step 1 :   PhoneWindow.setContentView();
// Activity 的setContentView() 最终会调用到 PhoneWindow 的 setContentView()
// 在 PhoneWindow 中开始真正加载布局
public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }

        // setContentView() 最终会通过LayoutInflater 去加载布局
        mLayoutInflater.inflate(layoutResID, mContentParent);
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
        ......
    }

Step 2 :   LayoutInflater.inflate();  // 解析给定的 XML 布局文件

 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
       .....
           ....
                    // Inflate all children under temp against its context.
                    //  注:在此处开始 inflateChild(),注: 最后一个参数为
                    // true , 代表是否接受 finishInflate() 回调。
                    rInflateChildren(parser, temp, attrs, true);

                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }
        ......    
    }

Step 3 :   LayoutInflater.rInflate();  // 递归解析布局

final int depth = parser.getDepth();
        int type;

        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
                ...........
                // 递归调用,直到布局解析完成为止。
                rInflateChildren(parser, view, attrs, true);
                viewGroup.addView(view, params);
            }
        }

        if (finishInflate) {
            // 注 : 此处 view 接收 onFinishInflate() 回调。
            parent.onFinishInflate();
        }

综上所述,在Activity setContentView()中,最终会调用到 LayoutInflate 中的 reInflate() 方法去递归解析布局,在布局解析完成以后,会调用 View.onFinishInflate() 回调。因此 View.onFinishInflate()调用时机在 setCotentView() 之后会立即调用到。所以如果有自定义 View 的话, findViewById() 可以放到该回调里面去做。

Activity.onResume() > View.onAttachedToWindow() :

在 ActivityThread 的 handleLaunchActivity() 中,Activity 的生命周期会依次得到回调,代码如下:

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
        
        注 : 在此处会执行 Activity onCreate() 、onStart() 回调。
        Activity a = performLaunchActivity(r, customIntent);

        if (a != null) {
            r.createdConfig = new Configuration(mConfiguration);
            reportSizeConfigurations(r);
            Bundle oldState = r.state;

            注 : 在此处会执行到 Activity onResume() 回调。
            handleResumeActivity(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
           .......
           }
}

从上述代码可以看出,从一个 Activity 的启动开始 handleLaunchActivity(),便依次执行了 onCreate()、onStart()、onResume() , 那么Activity 中 View 的测量绘制相关操作是在哪一步完成的呢。经常有听人回答到,在 onResume() 中,界面就真正可见可交互。但是这种方法是大错特错的,因为在 Activity onResume()中,View相关的测量绘制并没有开始! view 测量绘制真正开始时机是在 View.onAttachedToWindow()之后的。那么 onAttachedToWindow() 是什么时候调用的呢,看下一小节分析。

View.onAttachedToWindow() > View.onMeasure() :

在上一小节,可以看到在 Activity handleLaunchActivity() 中,最后会调用到 handleResumeActivity(),那么handleResumeActivity() 除了最后调用了 Activity onResume() 以后,具体还做了什么工作呢,代码如下:

final void handleResumeActivity(IBinder token,
                                    boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
        .......
        // TODO Push resumeArgs into the activity for consideration
        //  Step 1 :  注 : 该方法会执行 Activity onResume() 回调。
        r = performResumeActivity(token, clearHide, reason);

        if (r != null) {
            final Activity a = r.activity;

            // If the window hasn't yet been added to the window manager,
            // and this guy didn't finish itself or start another activity,
            // then go ahead and add the window.
            boolean willBeVisible = !a.mStartedActivity;
            if (!willBeVisible) {
                try {
                    willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
                            a.getActivityToken());
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }

            // Step 2 :  注 : 调用 WindowManager.addView() 将 Activity 
            对应的 window 添加到 WMS 中。
            if (r.window == null && !a.mFinished && willBeVisible) {
                r.window = r.activity.getWindow();
                View decor = r.window.getDecorView();
                decor.setVisibility(View.INVISIBLE);
                ViewManager wm = a.getWindowManager();
                WindowManager.LayoutParams l = r.window.getAttributes();
                a.mDecor = decor;
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
                l.softInputMode |= forwardBit;
                
                if (a.mVisibleFromClient && !a.mWindowAdded) {
                    a.mWindowAdded = true;
            // Step 3 :  注 : 调用 WindowManager.addView() 将 Activity 
            对应的 window 添加到 WMS 中。 decor 为 Activity 的根布局
                    wm.addView(decor, l);
                }
            } 
        } 

在 handleResumeActivity() 中,最终会调用到 WindowManager.addView() 去往 WSM 中添加 window ,最终该方法会 new 一个 ViewRootImpl 实例,并调用 requestLayout() 去请求布局,代码如下 :

 public void requestLayout() {
        if (!mHandlingLayoutInLayoutRequest) {
            checkThread();
            mLayoutRequested = true;
            scheduleTraversals();
        }
    }

void scheduleTraversals() {
        if (!mTraversalScheduled) {
            mTraversalScheduled = true;
            mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
            // 将 traversalRunnable 放到 messageQueue 中,准备执行
            mChoreographer.postCallback(
                    Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
            if (!mUnbufferedInputDispatch) {
                scheduleConsumeBatchedInput();
            }
            notifyRendererOfFramePending();
            pokeDrawLockIfNeeded();
        }
    }

在 viewRootImpl 执行 requestLayout() 的时候,会将mTraversalRunnable 以消息的形式存放于主线程的 MessageQueue中,mTraversalRunnable 中包含真正的view 测量、绘制等逻辑,mTraversalRunnable 的代码如下:


final class TraversalRunnable implements Runnable {
        @Override
        public void run() {
            doTraversal();
        }
    }

void doTraversal() {
        if (mTraversalScheduled) {
            mTraversalScheduled = false;
            mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);

            if (mProfile) {
                Debug.startMethodTracing("ViewAncestor");
            }
            // 在 performTraversals 中,开始真正执行 view 相关操作
            performTraversals();

            if (mProfile) {
                Debug.stopMethodTracing();
                mProfile = false;
            }
        }
    }

private void performTraversals() {
       ..... 
       // 注: host 为 decorView , 会依次分发,最后回调
       // 到 view onAttachedToWindow()
       host.dispatchAttachedToWindow(mAttachInfo, 0);
       .....
       // 测量
       performMeasure();
       .....
       // 布局
       performLayout();
       ......
       // 绘制
       performDraw();
       .....
}

综上可以看出,在 Activity onResume() 完成之后,会依次执行 view onAttachToWindow() , onMeasure()、onLayout()等。所以在 onResume() 中,并拿不到控件的宽高,因为此时,view 的测量根本都还没有开始!

相关文章

网友评论

    本文标题:Activity VS View

    本文链接:https://www.haomeiwen.com/subject/xetalftx.html