Oli Black
Oli Black

Reputation: 441

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 function in B, I have seen people mentioning to use

ExampleFragment fragment = (ExampleFragment)  
 getFragmentManager().findFragmentById(R.id.example_fragment);


fragment.<specific_function_name>();

I am having a problem though as my fragments are added with a tab listener not a fragmentmanager so I have no idea how to use this to call a method. Here is how my tabs are added.

public class fifaActivity extends Activity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    final ActionBar bar = getActionBar();
    bar.setTitle(TourneyName);
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setIcon(R.drawable.fifa);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        bar.addTab(bar
            .newTab()
            .setText("Tables")
            .setTabListener(
                    new TabListener<fifa.tables>(this, "tables",
                            fifa.tables.class)));

        bar.addTab(bar
            .newTab()
            .setText("Knockouts")
            .setTabListener(
                    new TabListener<fifa.knockouts>(this, "tables",
                            fifa.knockouts.class)));




    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex())

}

public static class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;

    public TabListener(Activity activity, String tag, Class<T> clz) {
        this(activity, tag, clz, null);
    }

    public TabListener(Activity activity, String tag, Class<T> clz,
            Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getFragmentManager()
                    .beginTransaction();
            ft.detach(mFragment);
            ft.commit();
        }
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                    mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }


}

Any help on how to call a method from the fifa.tables fragment to the fifa.knockouts fragment will be hugely appreciated.

Thanks, Oli

Upvotes: 0

Views: 1946

Answers (1)

Elad92
Elad92

Reputation: 2491

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:

public interface FragmentBMethodsCaller{
    void callTheMethodInFragmentB();
}

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

public class HostActivity extends Activity implements FragmentBMethodsCaller{
    ...
    public void callTheMethodInFragmentB(){
         --Implementation--
    }
    ...
}

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

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

Good luck.

Upvotes: 1

Related Questions