11

I have a parent fragment, within this. Upon a button click, a child dialog fragment is getting created. Now I would like to know how to call parent fragment function from child dialog fragment.

Here is the sample code :

/**SampleFragment.java**/

public class SampleFragment extends Fragment {
    // Instantiate view & add event handlers    

    public void onButtonClick(....) {
        // Create a dialog framgent
    }

    public void refreshView() {

    }
}

/**SampleDialogFragment.java**/

public class SampleDialogFragment extends DialogFragment {
    // Instantiate view for dialog

    public void onButtonClick(...) {
        // Call parent fragment method, i.e call refreshView() of SampleFragment 
    }
}
ALXKAY
  • 21
  • 1
  • 7
Naruto
  • 9,476
  • 37
  • 118
  • 201

4 Answers4

17

In a Fragment:

SampleDialogFragment dialogFragment = new SampleDialogFragment();
dialogFragment.show(getChildFragmentManager());

In a DialogFragment:

((SampleFragment) getParentFragment()).refreshView();

After calling this method, you can access public methods of a parent fragment.

ALXKAY
  • 21
  • 1
  • 7
Anant Shah
  • 3,744
  • 1
  • 35
  • 48
  • 2
    getParentFragment() is coming NULL for me :( – Joonsoo Jan 16 '19 at 14:49
  • 1
    @Joonsoo in that case you are not calling this method inside your child fragment class, Hierarchy should be Activity->ParentFragment->ChildFragment and inside your "ChildFragment" you have to write this code. It is a simple typecast to access your parent class. – Anant Shah Jan 17 '19 at 19:44
  • 2
    The key is to use getChildFragmentManager, not getFragmentManager – algrid Feb 04 '20 at 17:24
10

In say your parent fragment, SettingsFragment for example. Note the setTargetFragment()

public void onButtonClick(....)
{
            PrefLanguageDialogFragment prefLang = PrefLanguageDialogFragment.newInstance();
            prefLang.setTargetFragment(SettingsFragment.this, 1337);
            prefLang.show(getFragmentManager(), "dialog");
}

In our dialog, note the getTargetFragment()

SettingsFragment frag = (SettingsFragment)getTargetFragment();
if(frag != null){
   frag.refreshSomething();
}
SteD
  • 13,909
  • 12
  • 65
  • 76
0

when you want add SampleFragment to your activity set it a tag, e.g "SampleFragment".

then

public void onButtonClick(...){

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    SampleFragment parent = (SampleFragment)fm.findFragmentByTag("SampleFragment");
    parent.refreshview();
}

have not test it but it may help:-)

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • Is there any easy ways like interface or some other way? – Naruto Nov 08 '14 at 04:45
  • you can also try [getParentFragment()](http://developer.android.com/reference/android/app/Fragment.html#getParentFragment%28%29) or the idea of [callback](http://developer.android.com/training/basics/fragments/communicating.html#Implement) – mmlooloo Nov 08 '14 at 04:49
  • 1
    getParentFragment() is returning NULL, but your technique is not working seems like callback is good, i m not getting idea of how to do – Naruto Nov 08 '14 at 05:30
0

The best way is to go for interface, declare an interface in nested fragment -

public interface checkingClickListener { public void checkingClickListener(String data); }

then attach this interface to parent fragment -

public void onAttachFragment(Fragment fragment)
    {
        try
        {
            clickListener = (checkingClickListener) fragment;

        } catch (ClassCastException e)
        {
              throw new ClassCastException(fragment.toString() + " must implement checkingClickListener");
        }
     }


     @Override
     public void onCreate(Bundle savedInstanceState)
     {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        this.mContext = getActivity().getApplicationContext();
        onAttachFragment(getParentFragment());
        ....
     }

you need to call this listener on some button click -

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.tv_submit:
            if (clickListener != null)
            {                
                clickListener.checkingClickListener("sending data");
            }
            break;
    }
}

Implement this interface in parent fragment -

public class Fragment_Parent extends Fragment implements Nested_Fragment.checkingClickListener
{
      ....
     @Override
     public void checkingClickListener(final List<Player> players_list)
     {
           FragmentManager fragmentManager = getChildFragmentManager();
           SomeOtherNestFrag someOtherNestFrag = (SomeOtherNestFrag)  fragmentManager.findFragmentByTag([Tag of your fragment which you should use when you add]);
           if(someOtherNestFrag != null)
           {
                // your some other frag need to provide some data back based on views.
                SomeData somedata = someOtherNestFrag.getSomeData();
                // it can be a string, or int, or some custom java object.
           }
     }
}

Hope this helps you.

Ashish Tamrakar
  • 810
  • 10
  • 24