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

mid*_*ght 8 android android-layout android-fragments

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

zap*_*apl 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以它确实几乎相同,但它

  • 支持动画
  • 支持backstack
  • View返回的from设置Fragment#onCreateView()GONE而不是容器
  • 如果您在那里添加片段,则会关注菜单