Design + Code an Android App from Scratch
Scrounge up some loose change to become a pro member, or login to your account to view this lesson.
Alright, join now. Or login to your account if you're already a pro member.In this video we show you how to add a Fragment to an activity programmatically with the help of the FragmentManager and FragmentTransaction. Create two Fragment classes and their layout in XML. Go to the Activity where you need to add the Fragments. To get an instance of the support FragmentManager call getSupportFragmentManager. We simply call beginTransaction using the object of the FragmentManager which we just retrieved. Now we add or replace Fragments by creating their objects in code and assigning an ID or TAG so that we can retrieve them later using the findFragmentById or findFragmentByTag methods.