gpt4 book ai didi

java - 布局切换时 View 会发生什么情况?

转载 作者:行者123 更新时间:2023-12-01 23:36:23 26 4
gpt4 key购买 nike

如果我使用 setContentView 切换 Activity 的布局,之前布局中的 View 会发生什么情况?当布局切换时它们会被销毁并再次创建吗?

最佳答案

是的,从窗口的实现中可以看到,每次调用setcontentview(),之前的 View 都被移除了。

@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mLayoutInflater.inflate(layoutResID, mContentParent);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}

更深入...您可以看到所有 subview 都被删除并取消引用。然后将它们留给 GC。

public void removeAllViewsInLayout() {
final int count = mChildrenCount;
if (count <= 0) {
return;
}

final View[] children = mChildren;
mChildrenCount = 0;

final View focused = mFocused;
final boolean detach = mAttachInfo != null;
View clearChildFocus = null;

needGlobalAttributesUpdate(false);

for (int i = count - 1; i >= 0; i--) {
final View view = children[i];

if (mTransition != null) {
mTransition.removeChild(this, view);
}

if (view == focused) {
view.clearFocusForRemoval();
clearChildFocus = view;
}

if (view.getAnimation() != null ||
(mTransitioningViews != null && mTransitioningViews.contains(view))) {
addDisappearingView(view);
} else if (detach) {
view.dispatchDetachedFromWindow();
}

onViewRemoved(view);

view.mParent = null;
children[i] = null;
}

if (clearChildFocus != null) {
clearChildFocus(clearChildFocus);
}
}

关于java - 布局切换时 View 会发生什么情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com