0

を使用してアクティビティのレイアウトを切り替えるとsetContentView、以前のレイアウトのビューはどうなりますか? レイアウトを切り替えると、それらは破棄されて再び作成されますか?

4

2 に答える 2

2

はい、ウィンドウの実装から、setcontentview() を呼び出すたびに、以前のビューがすべて削除されることがわかります。

@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();
    }
}

さらに奥深く...すべての子ビューが削除され、参照解除されていることがわかります。そして、それらは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);
    }
}
于 2013-09-05T06:17:42.510 に答える
1

はい、すべてのビューはゼロから作成されます。レイアウト内のビューがリサイクルされるとは思いません。

于 2013-09-05T06:12:52.103 に答える