‫In this video.
‫I want from you to focus with me because I'm going to lecture a very important topic, which is the
‫fragment transaction.
‫I'll start defining the references of these buttons in the main activity button.
‫And I'll create a new method to be called when the user clicks on the button one or button two.
‫In order to start fragment transaction, it's called Load the Fragment and it took into the parameter
‫fragment type in order to display fragments.
‫I'll use fragment transaction and the fragment manager, fragment manager and fragment transaction are
‫classes used to manage and manipulate fragments within an activity.
‫They play a crucial role in dynamically adding, replacing, removing and navigating between fragments.
‫Let's dive into each of these classes.
‫I'll start with the fragment manager FM equal to get support Fragment Manager.
‫Fragment Manager is a system service provided by Android that is responsible for managing the fragments
‫within an activity.
‫It keeps track of all fragments and their states, allowing you to interact with them programmatically.
‫You can obtain a fragment manager instance by calling get support fragment manager for support based
‫fragments and this is for API level above 28.
‫And also you can use get fragment manager method, but this is not for API level or apps that use the
‫API and target the API 28 and above.
‫This is for 27 and below I'll use get support fragment manager.
‫The second step is to create a fragment transaction feet equal FM dot begin transaction fragment transaction.
‫The primary purpose of the fragment manager is to create and manage fragment transactions.
‫A transaction is a set of operations that involve adding, replacing or removing fragments within the
‫activity, and the Begin transaction creates a new transaction to change a fragment at runtime, you
‫can use add method and replace method for fragment transaction to specify which fragments to add or
‫replace and where to add them in the activities layout.
‫So I'll start with a fragment transaction dot replace and here I need to replace our ID dot frame layout.
‫I'm referring and specifying this frame layout in the main activity and I'll replace it with a fragment
‫that it is passed as a parameter.
‫Then I'll use FTE dot commit.
‫This method is used to save the changes.
‫I'll use this method load fragment in order to pass fragment number one and fragment number two when
‫the user clicks on the button number one and button number two respectively.
‫So button number one dot set on click listener new view on Click Listener.
‫And here I'm calling the method load fragment new first fragment.
‫Here I'm passing the first fragment instance the button number two dot set on Click Listener New new
‫view on click Listener Load the fragment, new second fragment, Save and run.
‫And here we go.
‫This is our Fragments application.
‫I'll click on display Fragment one and here we go.
‫This is our first fragment.
‫You can see on attach Oncreate and Onresume is called.
‫If I click on Say Hello, welcome to the first fragment.
‫So this is our first fragment.
‫The first layout here for the first fragment, you can see it.
‫And the second fragment.
‫Let me create display fragment two.
‫If I click on Say Bye Goodbye from Second Fragment.
‫So you can see this is how we navigate between the fragments and replace the fragments in Android.