7

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

quinestor
  • 1,432
  • 4
  • 19
  • 40

3 Answers3

6

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;
    }
makovkastar
  • 5,000
  • 2
  • 30
  • 50
1

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.

Community
  • 1
  • 1
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
0

In case anyone has tried all of the already suggested answers and is still facing the issue, like I was a few moments ago, the problem may come from a different source than your fragments.

In my case, I was switching between fragments correctly, but the reason for the blank screen was that in each new fragment I was trying to asynchronously load images by starting a new Thread every time (something a bit like this) instead of the recommended AsyncTask, or better yet the newSingleThreadExecutor, since AsyncTask is deprecated.

I disturbed the background enough that the fragment was just not showing up unless I navigated to a different app then back.

So unless your mistake is similar to mine my suggestion might be kind of vague but try to see if anything is happening in your fragment that may be intensive on the resources (commenting out different pieces of code may help in investigating this).

ribbit
  • 1,183
  • 11
  • 14