Communication is always important for developers. Sometimes we need to communicate between two fragments. Two fragments should not communicate directly as fragment is a re-useable UI component and should be built independently.
Now, we will see how we can communicate between two fragments using an interface.
Suppose, we have created two fragments in an activity named
FragmentB. We want to perform some task in
FragmentB has inserted some data in database.
First, we will define an interface in
Now, when the
FragmentB is attached to the
MainActivity, we will do the following :
After insertion some data in database in
FragmentB, we will use the callback interface to send message in
Now, we will implement the
Callback interface in
Callback event comes here from
FragmentB after inserting into database. Here, we will perform the task in
FragmentA through the method
FragmentA. For this, we will find the
FragmentA by its Tag and perform tasks on