Fragment Intermediate(III): Creating a activity that alternate between fragments onclick of respective button

后端 未结 2 483
孤城傲影
孤城傲影 2021-01-23 14:10

Aim:

Create a activity that have two buttons, button 1 and button 2. When click, the fragment will alternate between the two fragments.

Background:

Fragm

2条回答
  •  余生分开走
    2021-01-23 14:35

    While you add your Fragments dynamically, you keep your FragmentTwo inside your layout. Try to remove this in your layout:

      
    

    Also, in your code, you add dynamically a Fragment and with replace method, you set the container as id fragment_place:

    fragmentTransaction.replace(R.id.fragment_place, fr);  
    

    However, this id is using by your fragment (FragmentTwo, the first piece of code above) and you cannot replace a fragment which is not added dynamically. You need to host your fragment inside a FrameLayout but yours has no id, try to add this:

      
    

    Then, you will be able to host, add and replace your fragments into your framelayout as you want.

提交回复
热议问题