Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction of fragments in android results in blank screen

If it helps, what I want is similar to what is done in this google tutorial

But there a fragment is created prior to the transition. If I do that the transition works fine; but I can't use this approach

=====

Aiming to API 7+ I am just trying to have one Fragment visible in the whole screen and using a button (a drawn button, with an onTouch event) then alternate to a second fragment and viceversa.

But I either get a blank screen when I replace the first fragment with the second, or if I use fragmentTransaction.show and fragmentTransaction.hide; I can switch two times before I get blank screen. I dont want to have on backstack.

I am creating the fragments in MainActivity's onCreate:

DiceTable diceTable = new DiceTable();
Logger logger = new Logger();
fragmentTransaction.add(diceTable, DICETABLE_TAG);
fragmentTransaction.add(logger, LOGGER_TAG);
fragmentTransaction.add(R.id.fragment_container, logger);
fragmentTransaction.add(R.id.fragment_container, diceTable);

Then on one method (called from the fragments) I do the switch:

    Logger logger = (Logger)fragmentManager.findFragmentByTag(LOGGER_TAG);
    DiceTable diceTable = (DiceTable)fragmentManager.findFragmentByTag(DICETABLE_TAG);

    if (diceTable.isVisible()) {
        fragmentTransaction.replace(R.id.fragment_container, logger);

        fragmentTransaction.commit();
        fragmentTransaction.hide(diceTable);
        fragmentTransaction.show(logger);
    }
    else if (logger.isVisible()) {
        fragmentTransaction.replace(R.id.fragment_container, diceTable);

        fragmentTransaction.commit();
        fragmentTransaction.hide(logger);
        fragmentTransaction.show(diceTable);
    }

This is not how I should do this?

Blank screen when replacing fragments

like image 469
quinestor Avatar asked May 14 '13 14:05

quinestor


2 Answers

Try to initialize fragments in that way:

private void initFragments() {
    mDiceTable = new DiceTable();
    mLogger = new Logger();
    isDiceTableVisible = true;

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_container, mDiceTable);
    ft.add(R.id.fragment_container, mLogger);
    ft.hide(mLogger);
    ft.commit();
}

And then flip between them in that way:

 private void flipFragments() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (isDiceTableVisible) {
            ft.hide(mDiceTable);
            ft.show(mLogger);
        } else {
            ft.hide(mLogger);
            ft.show(mDiceTable);
        }
        ft.commit();
        isDiceTableVisible = !isDiceTableVisible;
    }
like image 159
makovkastar Avatar answered Sep 30 '22 12:09

makovkastar


You are combining two different methods of changing which Fragment is shown:

  • Calling replace() to replace the contents of the container with a different Fragment
  • Calling hide() to remove a Fragment, then calling show() to show another Fragment.

Pick one method and stick with it. The Building a Flexible UI guide uses just the replace() method, so I would start by trying to remove all of your calls to show() and hide().

Also see Android Fragments: When to use hide/show or add/remove/replace? for a quick summary of when it might be beneficial to use hide/show instead of replace.

like image 41
Bryan Herbst Avatar answered Sep 30 '22 12:09

Bryan Herbst