Skip to content Skip to sidebar Skip to footer

Getting Information From Dialogfragment Using Ondismiss()

I am working on an app and I am using a custom dialog which extends DialogFragment. This dialog will contain certain field that I want to pass to the parent activity. I tried imple

Solution 1:

Use a custom listener, below is an example on how this could be implemented. This is also explained in the Android Developer Guide.

publicclassCustomDialogextendsDialogFragment {

   publicinterfaceCustomListener{
      voidonMyCustomAction(CustomObject co);
   }

   private CustomListener mListener;

   publicvoidsetMyCustomListener(CustomListener listener){
     mListener = listener;
   }

   @Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
      ...
      Code to create dialog 
      ...
   }

   @OverridepublicvoidonDismiss(DialogInterface dialog) {
       if(mListener != null){
          CustomObjecto=newCustomObject();
          mListener.onMyCustomAction(o);
       }
       super.onDismiss();
   }
}

And when the custom dialog is created, set the listener.

CustomDialogawesomeDialog=newCustomDialog();
awesomeDialog.setMyCustomListener(newCustomDialog.CustomListener() {
  @OverridepublicvoidonMyCustomAction(CustomObject o){
     Log.i("TAG",o.toString());
  }
});

Post a Comment for "Getting Information From Dialogfragment Using Ondismiss()"