Transaction of fragments in android results in blank screen

前端 未结 2 911
灰色年华
灰色年华 2020-12-20 18:45

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 t

相关标签:
2条回答
  • 2020-12-20 19:10

    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.

    0 讨论(0)
  • 2020-12-20 19:24

    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;
        }
    
    0 讨论(0)
提交回复
热议问题