Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between fragmentTransaction.hide and setVisibility(GONE);

Is there any principal difference between setting layout's visibility that contains fragment to GONE and fragmentTransaction.hide(fragment) apart from addToBackStack ?

like image 262
midnight Avatar asked Jul 26 '12 13:07

midnight


People also ask

What is FragmentTransaction?

At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.

What is addToBackStack in android?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

How to replace one fragment with another in android?

setArguments(args); FragmentTransaction transaction = getSupportFragmentManager(). beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction. replace(R.


1 Answers

fragmentTransaction.hide(fragment) does

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

So it does pretty much the same but it

  • supports animation
  • supports backstack
  • sets the View returned from Fragment#onCreateView() to GONE instead of the container
  • takes care for the menu if you fragment added sth there
like image 169
zapl Avatar answered Oct 01 '22 09:10

zapl