Skip to content Skip to sidebar Skip to footer

Calling Method Inbetween Fragments Which Are In Tabs

I currently have a few fragments which are all organised in tabs and i want fragment A to call a method in frag B. So I know I need to call the activity which then calls the functi

Solution 1:

You can just create an interface the will be implemented by the hosting Activity. The Fragment A will hold reference to the Activity as this interface instance, and when it wants to call the function in Fragment B it will call the function in the interface. In the interface implementation in the hosting Activity, the Activity will made a call to the function in Fragment B.

Edit:

I will show some examples.

You need to create an interface that 'Fragment A' will use to call method in 'Fragment B', for example:

publicinterfaceFragmentBMethodsCaller{
    voidcallTheMethodInFragmentB();
}

Now, you need to implement it. Let's say your activity is called HostActivity:

publicclassHostActivityextendsActivityimplementsFragmentBMethodsCaller{
    ...
    publicvoidcallTheMethodInFragmentB(){
         --Implementation--
    }
    ...
}

Now, last thing you need to call it inside Fragment A:

FragmentBMethodsCallerfbmc= (FragmentBMethodsCaller)getActivity();
fbmc.callTheMethodInFragmentB();

Good luck.

Post a Comment for "Calling Method Inbetween Fragments Which Are In Tabs"