除了 addToBackStack 之外,将包含 fragment 的布局可见性设置为 GONE
和 fragmentTransaction.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/