Call fragment events from activity

后端 未结 2 731
走了就别回头了
走了就别回头了 2020-12-15 11:38

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 popula

相关标签:
2条回答
  • 2020-12-15 12:15

    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!

    0 讨论(0)
  • 2020-12-15 12:25

    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.

    0 讨论(0)
提交回复
热议问题