I'am working on a Product Detail page that opens every time the user clicks on a product on a list. In the page i have some product image at the top, and then 3 tabs below the image, and a fragment container below the tab to switch between the tabs.
The Problem: First time i load the Activity i call the showFragment(..)
method, and it populate the fragment_container
with the InfoFragment. When i then click on another tab and call the showFragment()
method again, the container becomes blank. The funny thing is that when i go back to the list and click the same product again, it loads the infoFragment
in the container as before, but when i click on the tabs they switch like butter (without problems).. Anyone have an idea of what is going wrong??
This is the method i call in onCreate()
that shows the infoFragment at start:
showFragment(R.id.product_detail_fragment_container, mInfoFragment, INFO_TAG, mCurrentFragmentTag, false);
When i click on a different (2nd) tab i call the same method with different parameters:
showFragment(R.id.product_detail_fragment_container, mCaraFragment, CARA_TAG, mCurrentFragmentTag, false);
And the showFragment(...)
method:
protected void showFragment(int resId, Fragment fragment, String tag, String lastTag, boolean addToBackStack)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (lastTag != null) {
Fragment lastFragment = fragmentManager.findFragmentByTag(lastTag);
if (lastFragment != null) {
transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
transaction.hide(lastFragment);
}
}
if (fragment.isAdded()) {
transaction.show(fragment);
} else {
transaction.add(resId, fragment, tag).setBreadCrumbShortTitle(tag);
}
if (addToBackStack) {
transaction.addToBackStack(tag);
}
transaction.commit();
}
XML for the container:
<FrameLayout
android:id="@+id/product_detail_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" />
I have tried debugging the code, and it calls the transaction.add(resId, fragment, tag)
method when i switch tab, but the container becomes blank. Then i close and start the detail page again, the method gets called and it shows the content... When i then try with another product it wont show the fragment, until i go back and click on it again