Home » Android » android – Call fragment events from activity

android – Call fragment events from activity

Posted by: admin June 15, 2020 Leave a comment


So, I got the event in my fragment to pass to the activity, so how do I make it so the activity in turns, notifies fragment B to do something. I want to fragment B to populate a custom list when fragment A has a list item clicked on. So, it sends the event to the activity, now how do I get the activity to call events in fragment B?

How to&Answers:

One way to do it would be like this in your activity:

FragmentB fragmentB = (FragmentB)getFragmentManager().findFragmentById(R.id.fragmentBId);

This is of course assuming that you have a publicly accessibly method in FragmentB called performSomeTask();

Hope that helps!


The best practice is probably to create interfaces for both fragments and then have the activity implement the interfaces. You want to have good decoupling between fragments so that you can reuse them in other places.