Android Fragments: When to Use Hide/Show or Add/Remove/Replace

Android Fragments: When to use hide/show or add/remove/replace?

You should consider what you plan to do with the fragment to decide which path to follow. If you use a FragmentTransaction to hide the fragment, then it can still be in the running state of its lifecycle, but its UI has been detached from the window so it's no longer visible. So you could technically still interact with the fragment and reattach its UI later you need to. If you replace the fragment, the you are actually pulling it out of the container and it will go through all of the teardown events in the lifecycle (onPause, onStop, etc) and if for some reason you need that fragment again you would have to insert it back into the container and let it run through all of its initialization again.


If there is a high probability that you will need that fragment again, then just hide it because it's a less expensive operation to redraw it's layout than to completely reinitialize it.

Show/Hide Fragment instead of replace

FragmentTransaction has the following methods you can use.

show(Fragment fragment)
hide(Fragment fragment)
add(int containerViewId, Fragment fragment, String tag)

And I think you don't need to call transaction.addToBackStack(s) in your case.

Situation where you would use FragmentTransaction.replace instead or show/hide or detach/reattach?

It maybe useful, for example, when implementing a deep fragments hierarchy in Multi-pane pattern (when click on item in the right fragment moves it to the position of the left).

Also, since hiding a Fragment keeps it in FragmentManager, it maybe expensive if you have a heavy content in it or hide multiple instances. Calling remove() or replace() and properly saving fragment's state is more Android-way, I think.

Android: Hide/show or add/remove fragments

Your framelayout has android:visibility="gone". On the code i don't see FragmentContainer.setVisibility(View.VISIBLE);

Just make changes to both of your if/else conditons...

if (FragmentContainer.getVisibility() == View.VISIBLE) {
getSupportFragmentManager().beginTransaction().remove(new FragmentOne()).commit();
FragmentContainer.setVisibility(View.GONE);
} else {
getSupportFragmentManager().beginTransaction().add(R.id.FragmentContainer, new FragmentOne()).commit();
FragmentContainer.setVisibility(View.VISIBLE);
}

How to Show/Hide an Android Fragment correctly?

Heres how I fixed this issue:-

First remove my overriden onBackPressed()

Change display ListFragment to this:-

final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);

fragmentTransaction.commit();

Change display detailFragment to this:-

mDetailFragment = new DetailFragment();

final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);

final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if (mLandscape) {
fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
fragmentTransaction.hide(mListFragment);
fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
fragmentTransaction.addToBackStack(DETAIL_FRAGMENT_TAG);
}

fragmentTransaction.commit();


Related Topics



Leave a reply



Submit