Reputation: 1925
I am trying to add and Remove a fragment in a relative layout. If in the relative layout (fragment container) is empty then add the fragment if not replace the fragment .
How to check whether the layout is empty or not , so that i do not get the error saying fragment already added.
Upvotes: 0
Views: 810
Reputation: 126
try this code
YourFragment dFrag = (YourFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailfragment);
if (dFrag != null && dFrag.isInLayout()) {
// do something
} else {
// do something
}
Upvotes: 2
Reputation: 2499
Not sure if this would be an answer, but i'd recommend you to use FragmentTransaction like this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if(yourOldFragment.isAdded()) {
ft.replace(R.id.your_container, yourNewFragment);
ft.commit();
}
You can also have a look this link : Fragment duplication on Fragment Transaction
Upvotes: 1