8

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

4

1 回答 1

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返回的 fromFragment#onCreateView()GONE不是容器
  • 如果您在此处分段添加某物,请注意菜单
于 2012-08-29T18:20:13.747 回答