0

I am having a DialogFragment like this,

public class Dfragment extends DialogFragment {

    public Dfragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
        ab.setMessage("Delete Everything").setTitle("DELETE DB")
                .setPositiveButton("OK",new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })

                .setNegativeButton("CANCEL",new DialogInterface.OnClickListener()

                { @Override
                    public void onClick(DialogInterface dialog, int which) {         }
                });


        return ab.create();
    }

}

I can detect the positive/negative from within the Fragment, but how do I detect it from an Activity from where it had been called.

Here is the code implemented in the Activity,

 Dfragment frag = new Dfragment();
        frag.show(getFragmentManager(),"THISDIALOG");

So in the Activity how do I determine which button has been clicked ?

Things I tried so far,

From the above Fragment code,

setPositiveButton("OK",null) replaced in the above code but still did not work.

What code will I need to modify to make it work as desired ?

EDIT 1:

I know it can be achieved by interfaces but I would like to do it without that.

oldcode
  • 1,669
  • 3
  • 22
  • 41
  • as usual 1. get activity, 2. cast to the interface 3. call the method from interface and pass which button was clicked ... – Selvin Nov 18 '16 at 12:21
  • 1
    Possible duplicate of [Communicating between a fragment and an activity - best practices.](http://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices) – Selvin Nov 18 '16 at 12:22
  • @Selvin Edited my question. – oldcode Nov 18 '16 at 12:25

3 Answers3

3

I have one suggetion for you. You can do like:

public abstract class Dfragment extends DialogFragment {

    public Dfragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
        ab.setMessage("Delete Everything").setTitle("DELETE DB")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onPositiveButtonClick();
                    }
                })

                .setNegativeButton("CANCEL", new DialogInterface.OnClickListener()

                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onNegativeButtonClick();
                    }

                });


        return ab.create();
    }

    public abstract void onPositiveButtonClick();

    public abstract void onNegativeButtonClick();

}

And Create object

Dfragment frag = new Dfragment() {
    @Override
    public void onPositiveButtonClick() {

    }

    @Override
    public void onNegativeButtonClick() {

    }
};
frag.show(getFragmentManager(),"THISDIALOG");
Ankita Shah
  • 1,866
  • 16
  • 31
0

You do not need to detect it from activity. Just write whatever your code is in that listeners and do call the same way you are calling. for example..

  @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
    ab.setMessage("Delete Everything").setTitle("DELETE DB")
            .setPositiveButton("OK",new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   Log.d("log", "this will log anyway");
                }
            })

            .setNegativeButton("CANCEL",new DialogInterface.OnClickListener()

            { @Override
                public void onClick(DialogInterface dialog, int which) {         }
            });


    return ab.create();
}

apart from this if you want to do any thing else than you need to implement a callback

good luck

AJay
  • 1,233
  • 10
  • 19
0

Use interface for this,

public class Dfragment extends DialogFragment {

Clicks objclick;

public Dfragment() {
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
    ab.setMessage("Delete Everything").setTitle("DELETE DB")
            .setPositiveButton("OK",new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                     obj.onclick();
                }
            })

            .setNegativeButton("CANCEL",new DialogInterface.OnClickListener()

            { @Override
                public void onClick(DialogInterface dialog, int which) {         }
            });


    return ab.create();
}

public void clicklistner(Clicks click){
    objclick=click;
}


  }

make an interface,

public interface Clicks{
    public void onclick();
}

call clicklistner method from your activity,

frag.clicklistner(new....
Sahil Garg
  • 263
  • 1
  • 20