fragmentTransaction.hide和setVisibility(GONE)之间的区别;

时间:2012-07-26 13:51:58

标签: android android-layout android-fragments

除了addToBackStack之外,将包含片段的布局的可见性设置为GONEfragmentTransaction.hide(fragment)之间是否有任何主要区别?

1 个答案:

答案 0 :(得分:8)

fragmentTransaction.hide(fragment)

public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
    if (DEBUG) Log.v(TAG, "hide: " + fragment);
    if (!fragment.mHidden) {
        fragment.mHidden = true;
        if (fragment.mView != null) {
            Animator anim = loadAnimator(fragment, transition, true,
                    transitionStyle);
            if (anim != null) {
                anim.setTarget(fragment.mView);
                // Delay the actual hide operation until the animation finishes, otherwise
                // the fragment will just immediately disappear
                final Fragment finalFragment = fragment;
                anim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (finalFragment.mView != null) {
                            finalFragment.mView.setVisibility(View.GONE);
                        }
                    }
                });
                anim.start();
            } else {
                fragment.mView.setVisibility(View.GONE);
            }
        }
        if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
            mNeedMenuInvalidate = true;
        }
        fragment.onHiddenChanged(true);
    }
}

所以它几乎一样,但它

  • 支持动画
  • 支持backstack
  • 将从View返回的Fragment#onCreateView()设置为GONE而不是容器
  • 如果你在那里添加片段
  • ,请注意菜单