SHARE
TWEET

Untitled

a guest Apr 21st, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class CustomDialogFragment extends DialogFragment {
  2.  
  3.     private DialogInterface.OnDismissListener mOnDismissListener;
  4.  
  5.     @Nullable
  6.     @Override
  7.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  8.         // can use databinding is better if need to bind event or content
  9.         return inflater.inflate(R.layout.fragment_simple_dialog, container, false);
  10.     }
  11.  
  12.     // ------ My apologize don't do this as Fragment require callback to clean the state and it will override anyway ------
  13.     @NonNull
  14.     @Override
  15.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  16.         // Dialog dialog = super.onCreateDialog(savedInstanceState);
  17.         // dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  18.         //    @Override
  19.         //    public void onDismiss(DialogInterface dialog) {
  20.         //        if (mOnDismissListener != null) {
  21.         //            mOnDismissListener.onDismiss(dialog);
  22.         //        }
  23.         //    }
  24.         // });
  25.         // return dialog;
  26.         return super.onCreateDialog(savedInstanceState);
  27.     }
  28.  
  29.     // ----- do this instead -----
  30.     @Override
  31.     public void onDismiss(DialogInterface dialog) {
  32.         super.onDismiss(dialog);
  33.         // can call "getActivity().finish();" directly but I afraid it can cause a problem
  34.         if (mOnDismissListener != null) {
  35.             mOnDismissListener.onDismiss(dialog);
  36.         }
  37.     }
  38.  
  39.     public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener){
  40.         mOnDismissListener = onDismissListener;
  41.     }
  42.  
  43. }
RAW Paste Data
Top