Search code examples
androidandroid-fragmentsvisibilityfragmenttransaction

Why does FragmentTransaction work sporadically?


I have several fragments in my xml (4 of them). The first time I run the activity with this code:

private void loadSenderFragment(int sender_fragment) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    // TODO: Animation for later
    if (loadRunOnce) {
        //ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);
    }

    switch (sender_fragment) {
    case FILES_TO_SEND_FRAGMENT:
        Log.i(StaticValues.TAG, "files to send fragment visisble");
        ft.hide(fragmentSendDev);
        ft.show(fragmentFilesSend);
        break;
    case SEND_TO_FRAGMENT:
        Log.i(StaticValues.TAG, "hiding filesSend, loading senddev");
        ft.hide(fragmentFilesSend);
        ft.show(fragmentSendDev);
        break;
    }
    if (loadRunOnce)
        ft.addToBackStack(null);
    else {
        ft.hide(fragmentReceiveWait);
        ft.hide(fragmentReceiving);
        loadRunOnce = true;
    }
    ft.commit();
}

It shows the proper view (Files_to_send_fragment), then from that fragment via a callback I call loadSenderFragment again except this time with the case of SEND_TO_FRAGMENT. I know that this gets called because my log : hiding fileSend, loading senddev shows up on my logcat and the ft.addToBackStack works as well because pressing theback button does not cancel the activity this is in . But the layout from fragmentFilesSend keeps showing while the other does not not(a page with a white background currently). From what I understand from my code and my intention is, hide filessend and show senddev. Does anyone know why that hide/show might not be working.

BTW I am using the compatibility library. (also tried the regular api Honeycomb+ library and still nothing).


Solution

  • I have figured out why this happens. For some reason if you use a style that has no window background, even though you hide/show different fragments all of them will be drawn (for some reason I have no idea why). So my style which I added a

    <item name="windowBackground">@null</item>
    

    in order to reduce how many pixels are drawn, was the thing that was blocking me. Who knew.