In the last Article you saw FrameLayout which helps to replace a Fragment. Now to do that you will need FragmentTransaction.
You can replace the Fragment at Runtime using FragmentTransaction.
Let’s see how.
Refer to the following code snippet which is taken from the code available here (https://github.com/ankur-srivastava/EmployeeApp/blob/master/app/src/main/java/com/edocent/employeeapp/MainActivity.java)
//Start Fragment Transaction
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.detailFragmentId, employeeDetailFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
The first line of code is used to get FragmentTransaction from FragmentManager. Once you have this you can write the code to replace the fragment.
This includes the replace method where you replace the FrameLayout ID with the new Fragment object.
There are other attributes which can be set using the FragmentTransaction object. Like the Transition attribute. For more details please refer to the Android Documentation.
Finally the commit method is used to complete the transaction.
Using this you will be able to replace the Fragment with a new object. This will be useful when the user will click on the back button.