‫Now let's add the functionality for this application.
‫When the user clicks on the Show Fragment one button, we need to display the fragment number one here
‫in the frame layout of the main activity.
‫Similar for the Show Fragment two button, we're going to display the fragment number two here in the
‫frame layout of the main activity.
‫For that I'm going to the main activity.
‫I'm going to initialize the widgets here in the Oncreate method.
‫So val button one it's of type button equals to find view by id alt plus enter to import the class of
‫button or the id dot display fragment one val button number two it's of type button equals to find view
‫by id r.id dot display fragment number two I want from you to focus with me because I'm going to lecture
‫a very important concept here, which is the fragment transaction.
‫I'll create a new method called load fragment.
‫It takes into the parameter fragment of type fragment.
‫At first we need to use something called fragment manager.
‫The fragment manager is responsible for managing fragments in an activity.
‫It keeps track of the fragment lifecycle and handles transactions involving fragments.
‫You can obtain an instance of Fragment Manager using Support Fragment Manager in an app compat activity.
‫So here I'll use Fragment Manager equals to support fragment Manager.
‫This is our fragment manager.
‫Another concept that we're going to use is the fragment transaction.
‫The fragment transaction class is used to define a set of operations to be performed on fragments.
‫It's obtained by calling begin transaction method on a fragment manager instance.
‫Common operation include adding, replacing, removing fragments and managing the backstack of fragments.
‫I'll create a fragment transaction val fragment transaction.
‫It's of type fragment transaction equals to fragment manager dot begin.
‫Transaction begin.
‫Transaction method initiates a new fragment transaction.
‫You can perform operations like adding, replacing or removing fragments within a transaction.
‫You can use other methods like find the fragment by ID method or find the fragment by tag method.
‫This will retrieve a fragment by its ID or tag, but we are interested in begin transaction method.
‫So at first we created the fragment manager.
‫The second step is to create fragment transaction, which will use the fragment manager to get the begin
‫transaction method and returns an instance of fragment transaction.
‫The third step replace the frame layout of the main activity.
‫This frame layout.
‫Replace it with a fragment with the chosen fragment, and the chosen fragment will be passed as a parameter
‫for this method here I'm going to use fragment transaction dot replace method.
‫I'm going to replace r.id dot frame layout with the newly passed fragment in the parameter of this method.
‫So guys, when calling this load fragment method, I'm going to pass the fragment that I'm going to
‫inflate to display.
‫And here fragment transaction dot replace where replace the frame layout of the main activity with the
‫fragment chosen.
‫Then I'm going to use a fragment transaction dot commit.
‫The commit function will commit the transaction applying the specified operation, which is the replacing
‫functionality.
‫Remember to commit transactions after defining the operations.
‫Also, transactions should generally be performed within the appropriate lifecycle methods of the activity
‫to ensure proper execution.
‫Again guys, we created this method.
‫We created this fragment manager.
‫The second step we create the fragment transaction and we called begin transaction method.
‫The third step is to replace the Framelayout with the new fragment that it is passed as a parameter
‫of this load fragment method.
‫And the last step is to commit the transaction.
‫Now let's call this function when the user clicks on the button one and button two.
‫So button one dot set on click listener.
‫And here I'm going to call the loader fragment.
‫And I'm going to create an instance of fragment number one.
‫It's of type.
‫Fragment one equals to fragment one creating this empty instance.
‫And I'm calling the load fragment function and passing fragment number one.
‫Also button number two dot set on click listener val.
‫Fragment two equals to fragment two.
‫I'm creating an empty instance of fragment two and load the fragment fragment number two.
‫This is how we create fragment instances and pass it to the load fragment.
‫Let's run our application.
‫Here we go.
‫This is our application.
‫I'll click on Show Fragment one and you can see that the fragment one is displayed inside this framelayout.
‫Click on show fragment number two and fragment one is replaced by fragment number two.
‫This is the fragment transaction in Android and how we performed the transaction using the Fragment
‫manager and the fragment transaction.