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
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.