最全onAttachedToWindow和onDetachedFromWindow的调用时机分析,2024年最新护士15个经典面试问题

结语

由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是目录截图:

由于整个文档比较全面,内容比较多,篇幅不允许,下面以截图方式展示 。

再附一部分Android架构面试视频讲解:

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

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

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

  • of this class, call {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.

*/

public interface ViewManager

{

/**

  • Assign the passed LayoutParams to the passed View and add the view to the window.

  • Throws {@link android.view.WindowManager.BadTokenException} for certain programming

  • errors, such as adding a second view to a window without removing the first view.

  • Throws {@link android.view.WindowManager.InvalidDisplayException} if the window is on a

  • secondary {@link Display} and the specified display can’t be found

  • (see {@link android.app.Presentation}).

  • @param view The view to be added to this window.

  • @param params The LayoutParams to assign to view.

*/

public void addView(View view, ViewGroup.LayoutParams params);

public void updateViewLayout(View view, ViewGroup.LayoutParams params);

public void removeView(View view);

}

而WindowManager的样子差不多是这样,如下图:

在这里插入图片描述

当在ActivityThread.handleResumeActivity()方法中调用WindowManager.addView()方法时,最终是调去了

WindowManagerImpl.addView() -->

WindowManagerGlobal.addView()

这里我们看下最终调用到的代码:

public void addView(View view, ViewGroup.LayoutParams params,

Display display, Window parentWindow) {

if (view == null) {

throw new IllegalArgumentException(“view must not be null”);

}

if (display == null) {

throw new IllegalArgumentException(“display must not be null”);

}

if (!(params instanceof WindowManager.LayoutParams)) {

throw new IllegalArgumentException(“Params must be WindowManager.LayoutParams”);

}

final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;

if (parentWindow != null) {

parentWindow.adjustLayoutParamsForSubWindow(wparams);

} else {

// If there’s no parent, then hardware acceleration for this view is

// set from the application’s hardware acceleration setting.

final Context context = view.getContext();

if (context != null

&& (context.getApplicationInfo().flags

& ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {

wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;

}

}

ViewRootImpl root;

View panelParentView = null;

synchronized (mLock) {

// Start watching for system property changes.

if (mSystemPropertyUpdater == null) {

mSystemPropertyUpdater = new Runnable() {

@Override public void run() {

synchronized (mLock) {

for (int i = mRoots.size() - 1; i >= 0; --i) {

mRoots.get(i).loadSystemProperties();

}

}

}

};

SystemProperties.addChangeCallback(mSystemPropertyUpdater);

}

int index = findViewLocked(view, false);

if (index >= 0) {

if (mDyingViews.contains(view)) {

// Don’t wait for MSG_DIE to make it’s way through root’s queue.

mRoots.get(index).doDie();

} else {

throw new IllegalStateException("View " + view

  • " has already been added to the window manager.");

}

// The previous removeView() had not completed executing. Now it has.

}

// If this is a panel window, then find the window it is being

// attached to for future reference.

if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&

wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {

final int count = mViews.size();

for (int i = 0; i < count; i++) {

if (mRoots.get(i).mWindow.asBinder() == wparams.token) {

panelParentView = mViews.get(i);

}

}

}

root = new ViewRootImpl(view.getContext(), display);

view.setLayoutParams(wparams);

mViews.add(view);

mRoots.add(root);

mParams.add(wparams);

}

// do this last because it fires off messages to start doing things

try {

// 这行代码是本文重点关注的!!!

root.setView(view, wparams, panelParentView);

} catch (RuntimeException e) {

// BadTokenException or InvalidDisplayException, clean up.

synchronized (mLock) {

final int index = findViewLocked(view, false);

if (index >= 0) {

removeViewLocked(index, true);

}

}

throw e;

}

}

其中有一句root.setView(view, wparams, panelParentView);,正是这行代码将调用流程转移到了ViewRootImpl.setView()里面,此方法内部最终会触发ViewRootImpl.performTraversals()方法,这个方法就是我们熟悉的View从无到有要经历的3个阶段(measure, layout, draw),不过这个方法内部和我们这里讨论的内容相关的是其1364行代码:host.dispatchAttachedToWindow(mAttachInfo, 0);,这里的host就是Act的DecorView(FrameLayout的子类),我们可以看到是通过这样的dispatch方法将这个调用沿着View tree分发了下去,我们分别看下ViewGroup和View中这个方法的实现,如下:

// ViewGroup中的实现:

void dispatchAttachedToWindow(AttachInfo info, int visibility) {

mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;

// 先调用自己的

super.dispatchAttachedToWindow(info, visibility);

mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW;

final int count = mChildrenCount;

final View[] children = mChildren;

for (int i = 0; i < count; i++) {

final View child = children[i];

// 递归调用每个child的dispatchAttachedToWindow方法

// 典型的深度优先遍历

child.dispatchAttachedToWindow(info,

combineVisibility(visibility, child.getVisibility()));

}

final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size();

for (int i = 0; i < transientCount; ++i) {

View view = mTransientViews.get(i);

view.dispatchAttachedToWindow(info,

combineVisibility(visibility, view.getVisibility()));

}

}

// View中的实现:

void dispatchAttachedToWindow(AttachInfo info, int visibility) {

//System.out.println("Attached! " + this);

mAttachInfo = info;

if (mOverlay != null) {

mOverlay.getOverlayView().dispatchAttachedToWindow(info, visibility);

}

mWindowAttachCount++;

// We will need to evaluate the drawable state at least once.

mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;

if (mFloatingTreeObserver != null) {

info.mTreeObserver.merge(mFloatingTreeObserver);

mFloatingTreeObserver = null;

}

if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {

mAttachInfo.mScrollContainers.add(this);

mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;

}

performCollectViewAttributes(mAttachInfo, visibility);

onAttachedToWindow();

ListenerInfo li = mListenerInfo;

final CopyOnWriteArrayList listeners =

li != null ? li.mOnAttachStateChangeListeners : null;

if (listeners != null && listeners.size() > 0) {

// NOTE: because of the use of CopyOnWriteArrayList, we must use an iterator to

// perform the dispatching. The iterator is a safe guard against listeners that

// could mutate the list by calling the various add/remove methods. This prevents

// the array from being modified while we iterate it.

for (OnAttachStateChangeListener listener : listeners) {

listener.onViewAttachedToWindow(this);

}

}

int vis = info.mWindowVisibility;

if (vis != GONE) {

onWindowVisibilityChanged(vis);

}

// Send onVisibilityChanged directly instead of dispatchVisibilityChanged.

// As all views in the subtree will already receive dispatchAttachedToWindow

// traversing the subtree again here is not desired.

onVisibilityChanged(this, visibility);

if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {

// If nobody has evaluated the drawable state yet, then do it now.

refreshDrawableState();

}

最后笔者收集整理了一份Flutter高级入门进阶资料PDF

以下是资料目录和内容部分截图



里面包括详细的知识点讲解分析,带你一个星期入门Flutter。还有130个进阶学习项目实战视频教程,让你秒变大前端。

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

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

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

录和内容部分截图

[外链图片转存中…(img-DJaYpZq7-1715412681263)]
[外链图片转存中…(img-JAwoUoeJ-1715412681263)]
里面包括详细的知识点讲解分析,带你一个星期入门Flutter。还有130个进阶学习项目实战视频教程,让你秒变大前端。

[外链图片转存中…(img-qtyuawtM-1715412681263)]

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

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

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

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值