marcoms10
marcoms10

Reputation: 71

How can I call a method from nested fragment

I have a fragment(this is one tab of my tabhost) "fragA" inside other fragment "fragB" but I need to call one method of "fragB" from "fragA". when I do from an activity, I do this:

FragB detailsFragment=(FragB)getSupportFragmentManager().findFragmentById(R.id.detailFragment);

detailsFragment.consultaWS(convertIntWebService(categoria),"Pdf");

please, help me

Upvotes: 4

Views: 3765

Answers (5)

Rishabh Mahatha
Rishabh Mahatha

Reputation: 1271

Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_main_container);
if (fragment != null) {      
       ((TabLayoutFragment)fragment).tabClickDisable();
}

Upvotes: 0

marcoms10
marcoms10

Reputation: 71

I could finally solve it this way, in fragA:

FragB parentFragment = (FragB) getChildFragmentManager()
                                      .findFragmentByTag("detallesFragBusqueda");
if (parentFragment != null) {
    parentFragment.consultaWS("", "AllPdf");
}

Upvotes: 3

zeshanbaig786
zeshanbaig786

Reputation: 157

Read this http://developer.android.com/training/basics/fragments/communicating.html. This document explains that how can you communicate between Activity and Fragment and also between 2 Fragments

Upvotes: 0

ILovemyPoncho
ILovemyPoncho

Reputation: 2792

If FragA is inside FragB, you can do the same from within your FragA.

 FragB parentFragment = (FragB)getActivity().getSupportFragmentManager().findFragmentById(R.id.detailFragment);
 parentFragment.fragmentBMethod();

Upvotes: 0

ashishduh
ashishduh

Reputation: 6699

Your activity should facilitate all communication between fragments. Just create a method in your Activity that a fragment can call, which will tell another fragment to do something.

Upvotes: 0

Related Questions