Reputation: 3518
I have this problem in an app, where that method below doesn't always effectively hide the currently visible fragment (contentFragment) and shows the new one (fragment). Basically, sometimes, it does the complete opposite of what it's supposed to do, that is, the current fragment remaining visible, and the new one not showing at all. And what is worth, is that I can't always replicate when it happens.
I'm really at wits' end here. Anyone got any ideas?
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(contentFragment);
transaction.commit();
fragmentManager.executePendingTransactions();
contentFragment = fragment;
}
Upvotes: 1
Views: 136
Reputation: 5251
Your contentFragment sometimes do not have the value you are expecting, try to retrieve the current fragment to ensure you are really hiding the fragment before your new transaction.
Modify your addContentFragment(GenericFragment fragment)
as the follows:
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.ContentLayout);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(currentFragment);
transaction.commit();
}
Upvotes: 1