Transaction of fragments in android results in blank screen

前端 未结 2 921
灰色年华
灰色年华 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条回答
  •  Happy的楠姐
    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;
        }
    

提交回复
热议问题