Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call fragment events from activity

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?

like image 243
Shaun Avatar asked Mar 06 '11 03:03

Shaun


People also ask

How do I call fragment from activity Onclicklistener in Android?

The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour... Creating a new Activity really defeats the whole purpose to use fragments anyway... very counter productive.


1 Answers

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

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

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

Hope that helps!

like image 97
runor49 Avatar answered Oct 11 '22 23:10

runor49