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

标签 android android-layout android-fragments

除了 addToBackStack 之外,将包含 fragment 的布局可见性设置为 GONEfragmentTransaction.hide(fragment) 之间有什么主要区别吗?

最佳答案

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

所以它的作用几乎相同,但是它

  • 支持动画
  • 支持后台
  • Fragment#onCreateView() 返回的 View 设置为 GONE 而不是容器
  • 如果您在其中添加了 fragment ,则负责菜单

关于android - fragmentTransaction.hide 和 setVisibility(GONE) 之间的区别;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670826/

相关文章:

Android:不推荐使用 onRetainNonConfigurationInstance()?

java - 使用 Gradle 的开发和生产环境的不同参数

android - 约束布局中的底纸行为

java - 我的一些代码没有在 android studio 中执行?

android - 全屏 Activity 背景图片的大小

android - 布局不向上滚动?

android - recyclerview header,从Activity/Fragment中的header访问对象

java - 无法点击 fragment 中的 ListView

android - 如何在触摸模式下找到Listview中的位置?

android - 将 SQLiteOpenHelper 与检测上下文一起使用时为 "Unable to open database file"