1

In my application I have a fragment which open a dialog fragment. In my dialog fragment I take in input from user some settings that in save in a static class, but I need a listener to inform my Fragment that the dialog was closed.

I use the code from the first answer of this link stackoverflow question but this part of code doesn't work because I have a fragment and not an activity. The onDismiss method is called in dialogfragment but the if statement fails and so doesn't call the fragment. I tried to replace that part with getFragmentManager and with getParentFragment and get/setTargetFragment but doesn't work.

Someone can help me please?

public void onDismiss(final DialogInterface dialog) {
    super.onDismiss(dialog);

    // code I've tried
    final FragmentManager fragment = getFragmentManager();
    if (fragment instanceof DialogInterface.OnDismissListener) {
        ((DialogInterface.OnDismissListener) fragment).onDismiss(dialog);
    }

    // original answer code  
    /*final Activity activity = getActivity();
    if (activity instanceof DialogInterface.OnDismissListener) {
        ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
    }*/

}
Community
  • 1
  • 1
pnappa
  • 75
  • 2
  • 11

3 Answers3

3

This is an old issue but I found no solution I am happy with. I don't like passing any Listeners to my DialogFragment or set a TargetFragment, because that may break on orientation change. What do you think about this?

    MyDialog d = new MyDialog();
        d.show(fragmentManager, "tag");
        fragmentManager.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
                @Override
            public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
                super.onFragmentViewDestroyed(fm, f);
                //do sth      
        fragmentManager.unregisterFragmentLifecycleCallbacks(this);
                }
            }, false);
Kaskasi
  • 1,330
  • 1
  • 14
  • 15
  • I completely change my code to find a solution, it was an old project that is finished 1 year ago so I can't try this new code to solve my old problem. Btw the code you posted should work and I also like the approce also you answerd to an old question. – pnappa May 20 '17 at 09:05
  • To be honest, I even found a better solution. The reason you cannot use getDialog on the DialogFragment and then set a dismiss listener is that the FragmentTransaction has not been executed. So when you executePendingTransactions on the FragmentManager getDialog will not return null. – Kaskasi May 28 '17 at 15:22
  • Without appcompat registerFragmentLifecycleCallbacks `Call requires API level 26` – Vlad Sep 17 '19 at 14:02
1

You can try interface call back,

create an interface in your dialog fragment

    public interface Listener{
     void onDismiss();
    }

implement this in your fragment,

public class MyFragment extends Fragment implements MyDialogFragment.Listener{

  @Override
public void onDismiss(){
  //write the operations here   
 }
}

now in your dialog fragment recieve the object of your parent fragment, ex:

public void setListener(Listener listener){
 this.mListener = listener;
}

you have to call this method from your fragment, to set the listener.

now in your onDismiss() method of your dialog fragment call onDismiss using this reference

ex

@Override
public void onDismiss(){
  mListener.onDismiss();
}

do the necessory null checks

check this below link too

How to get button clicks in host fragment from dialog fragment

Community
  • 1
  • 1
droidev
  • 7,352
  • 11
  • 62
  • 94
  • when I use setListener(this) doesn't work because I'm in a View.OnClickListener so doesn't accept "this". Any advice? – pnappa Nov 10 '15 at 13:25
  • I solved it, I use totally different approch. Thanks for your time. – pnappa Nov 12 '15 at 08:20
-1

I think you can another way to achieve it:

// create a function in YourFragment
public void myDissmiss();
// change your newInstance(bitmap) of your dialog fragment
public static DialogFragmentImage newInstance(Bitmap b,YourFragment f);

then you can call yourfragment.myDissmiss() in your public void onDismiss(final DialogInterface dialog) of DialogFragmentImage.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86