4

Normally, from an Activity I could invoke an object that extends DialogFragment and implements DialogInterface.OnClickListener().

Then if I wanted to, from that Dialog, invoke a method in the parent activity, I could do ((NameOfParentActivity)getActivity()).nameOfMethod();

But now I am invoking such an object from a Fragment (specifically, a page of a ViewPager) instead of an Activity.

I'm not exactly sure what to change to get the method-calling line to work. In other words I am trying to call a method from the Fragment class, while inside its DialogFragment object.

Edit: OnClick() code from my Adapter class:

public void onClick(View v) {
    DeleteGameBoardDialogFragment dialogFragment = new DeleteGameBoardDialogFragment();
    Bundle args = new Bundle();
    args.putString(TabFragmentGameBoard.GAMEBOARD_DIALOG_TAG, mGameBoards.get(position).getGameBoardName());
    dialogFragment.setArguments(args);
    dialogFragment.show(((Activity) mContext).getFragmentManager(), "");
}
Aleksandar G
  • 963
  • 2
  • 19
  • 23
AJJ
  • 1,794
  • 3
  • 22
  • 34

2 Answers2

3

You can solve your issue with setTargetFragment() and getParentFragment() to your dialog Fragment.

Call dialog from your ViewPager fragment, where you want execute a fragment method.

DeleteGameBoardDialogFragment dialogFragment = new DeleteGameBoardDialogFragment();
dialogFragment.setTargetFragment(this,1);
FragmentTransaction transaction=getChildFragmentManager().beginTransaction();//must be getChildFragmentManager()
dialogFragment.show(transaction, "df");

And inside of your DeleteGameBoardDialogFragment, call below code

DeleteGameBoardDialogFragment  fragment=(DeleteGameBoardDialogFragment) getParentFragment();
fragment.nameOfMethod()

IMPT: make sure you call dialog from a fragment only and not from activity. Why If you call from activity getParentFragment() returns null.

getParentFragment () : Returns the parent Fragment containing this Fragment. If this Fragment is attached directly to an Activity, returns null.. more visit SO : getParentFragment returning null

Bharatesh
  • 8,375
  • 3
  • 34
  • 61
  • Hi @Bharatesh after calling `setTargetFragment` in mainfragment, when I call this `((MainFragment)getTargetFragment()).method();` it is working. Is this a good way to do it – a0x2 Jan 17 '19 at 10:35
  • 1
    @a2en no issues.. only difference is that I have created a object and called a method and you don't.. however its your call how you do it. Code readability is important. – Bharatesh Jan 17 '19 at 12:15
  • but you are calling `getParentFragment();` and i called `getTargetFragment` – a0x2 Jan 17 '19 at 12:26
  • 1
    I missed it... Please check difference between [getParentFragment()](https://developer.android.com/reference/android/support/v4/app/Fragment.html#getParentFragment()) and [getTargetFragment()](https://developer.android.com/reference/android/support/v4/app/Fragment.html#getTargetFragment()) on official documentation. as both methods depends on implementing child fragment. – Bharatesh Jan 17 '19 at 12:40
0

From what I understood from your question, you have a ViewPager that opens a dialog when a button is clicked, and you wanted to access a method from your ViewPager Fragment?

If this is your question, might as well try making an Interface that behaves like a Listener and pass it on the DialogFragment. The Fragment where your method is, should implement this listener.

ekouChiq
  • 244
  • 2
  • 10