Luciano Stupenengo
Luciano Stupenengo

Reputation: 183

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 clicks the positive Button on my FragmentDialog I want to call a method in main activity. A simple idea, but execution is killing me.

My main Activity uses tabbed browsing, which I thing is messing the hole thing up.

This is a short version of my main activity.

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener  {

   //.....

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if(id == R.id.menu_change_date){
            DialogFragment dialog = new Dialog_Elegir_Mes();
            dialog.show(getSupportFragmentManager(),"Elegir Mes");
        }

  //....

  public void dosomething() {

   }

 }

Dialog

public class Dialog_Elegir_Mes extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers
        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        final View v = inflater.inflate(R.layout.diag_select_month,null);


        adb.setTitle("Elegir Mes");

        adb.setView(v)
                .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                          //Here call dosomething();
                    }
                })
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Dialog_Elegir_Mes.this.getDialog().cancel();
                    }
                });


        return adb.create();
    }
}

Upvotes: 0

Views: 1342

Answers (1)

Rohit5k2
Rohit5k2

Reputation: 18112

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

public interface OnMyDialogClick
{
    public abstract void onPositiveButtonClick();
}

Your activity

if(id == R.id.menu_change_date){
        DialogFragment dialog = new Dialog_Elegir_Mes(new OnMyDialogClick()
        {
              @Override
              public void onPositiveButtonClick()
              {
                   //Call your activity method here
              }
        });
        dialog.show(getSupportFragmentManager(),"Elegir Mes");
}

Your Dialog class

public class Dialog_Elegir_Mes extends DialogFragment {
private OnMyDialogClick _onMyDialogClick = null;
public Dialog_Elegir_Mes(OnMyDialogClick ref)
{
    _onMyDialogClick = ref;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Build the dialog and set up the button click handlers
    AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    final View v = inflater.inflate(R.layout.diag_select_month,null);


    adb.setTitle("Elegir Mes");

    adb.setView(v)
            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                      _onMyDialogClick.onPositiveButtonClick();
                }
            })
            .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Dialog_Elegir_Mes.this.getDialog().cancel();
                }
            });


    return adb.create();
}
}

Upvotes: 1

Related Questions