Skip to content Skip to sidebar Skip to footer

Communicating My Dialog With Main Activity

I have read a lot of tutorials on how to communicate a FragmentDialog with the activity but I seem be unable to adapt them to my project. What I want to do is simple, when the user

Solution 1:

Create an Interface and implement that in the Activity class and pass its reference to the Dialog class. When user clicks the dialog button call the method using the interface.

Do something like this

publicinterfaceOnMyDialogClick
{
    publicabstractvoidonPositiveButtonClick();
}

Your activity

if(id == R.id.menu_change_date){
        DialogFragmentdialog=newDialog_Elegir_Mes(newOnMyDialogClick()
        {
              @OverridepublicvoidonPositiveButtonClick()
              {
                   //Call your activity method here
              }
        });
        dialog.show(getSupportFragmentManager(),"Elegir Mes");
}

Your Dialog class

publicclassDialog_Elegir_MesextendsDialogFragment {
privateOnMyDialogClick_onMyDialogClick=null;
publicDialog_Elegir_Mes(OnMyDialogClick ref)
{
    _onMyDialogClick = ref;
}

@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
    // Build the dialog and set up the button click handlers
    AlertDialog.Builderadb=newAlertDialog.Builder(getActivity());
    LayoutInflaterinflater= getActivity().getLayoutInflater();

    finalViewv= inflater.inflate(R.layout.diag_select_month,null);


    adb.setTitle("Elegir Mes");

    adb.setView(v)
            .setPositiveButton("Aceptar", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int id) {

                      _onMyDialogClick.onPositiveButtonClick();
                }
            })
            .setNegativeButton("Cancelar", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int id) {
                    Dialog_Elegir_Mes.this.getDialog().cancel();
                }
            });


    return adb.create();
}
}

Post a Comment for "Communicating My Dialog With Main Activity"