2

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

Ibrahim Yildirim
  • 2,731
  • 2
  • 19
  • 31
  • Can we see the XML for the fragment container? – RoraΖ Jul 23 '14 at 19:25
  • I know this might be weird but I don't think you can do hide then add in the same commit. http://stackoverflow.com/questions/16546098/transaction-of-fragments-in-android-results-in-blank-screen Maybe try committing the hide, then adding and recommitting? – RoraΖ Jul 23 '14 at 19:33
  • you can not commit twice in the same transaction! I tried to `fragmentManager.beginTransaction.hide(oldFragment).commit();` and then add the other one with `transaction.commit()`, but it was the same issue. Blank screen the first time -> go back and open the same product -> works – Ibrahim Yildirim Jul 23 '14 at 20:58
  • What about performing add then hide, like in the answer I linked to? – RoraΖ Jul 24 '14 at 11:07
  • @IbrahimYildirim Did you find any solution? – Krupa Kakkad Jul 29 '19 at 07:06

0 Answers0