8

I'm creating a dialog as follows:

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DIALOG_1:
   return new AlertDialog.Builder(this)
   .setTitle(R.string.s_dlg1)
   .setPositiveButton(android.R.string.ok, this)
   .create();

  case DIALOG_2:
   ...
   ...
  }

  return null;
 }

 @Override
 public void onClick(DialogInterface dialog, int whichButton) {
  if (dialog == ???) {
   ...
  }
  else if (dialog == ???){
   ...
  }
 }

How do I identify which dialog triggered the onClick method? I can't declare the interface methods as in-line when creating the dialog because I want to access variables in my class. Every other interface passes some sort of id to its methods to identify which object called the method, but I can't seem to do anything with 'DialogInterface dialog'.

Monstieur
  • 7,992
  • 10
  • 51
  • 77

6 Answers6

1

Perhaps you can extract the onclick listener as a seperate class and then pass in the dialog id? The interface is android.content.DialogInterface.OnClickListener

Hans
  • 2,448
  • 2
  • 24
  • 30
  • That would amount to the same as storing it in a private variable in the current class, since only one dialog can be shown at once, and the onClick will fire before the user can do anything to start another dialog and change the value. That's what I'm doing now, but it's unclean. I'm having the same problem with multiple DatePickerDialog on the same activity. There doesn't seem to be a straightforward way to identify which one fired the onDateSet event. – Monstieur Feb 23 '10 at 04:18
  • i dont know of a better way, if you want to identify the dialog you have to store the id somewhere, now dialogs dont contain a "tag" or an id, so you have to either - implement the onclick in a class that receives a variable identifying the dialog, either extend the dialog class itself and add an id, or save in a variable in the calling fragment or activity and getting them using getContext. another way (not sure if it works) is to used dialogFragment and add a tag to it thereby you can get the tag and identify the dialog used. – Gabriel H Nov 05 '14 at 18:23
0

This question is quite old and has been unanswered so far, so I've found a solution for this exact issue I've also run into.

Inside this activity I've launched there's the possibility, depending on the user's actions, to fire 2 distinct custom DialogFragments. Let's call them DialogFragment A or B. We create these with the standard builder and then .show(fragmentManager, <TAG>) where tag could either be A_TAG or B_TAG to easily identify them later.

So either one of these DialogFragment is shown, you will want to decide what to do in your implemented onClick(DialogInterface dialog, int buttonClicked) and here's how I've done so:

final DialogFragment aDialogInterface = (DialogFragment) getSupportFragmentManager().findFragmentByTag(A_TAG);
final DialogFragment bDialogInterface = (DialogFragment) getSupportFragmentManager().findFragmentByTag(B_TAG);

if (aDialogInterface != null) {
            //do something for dialogfragment A
} else if (bDialogInterface != null) {
            //do something for dialogfragment B
}

basically doing a null-check on findFragmentByTag(...) ...

Marvin Effing
  • 2,693
  • 3
  • 20
  • 35
0

I have met with the same issue. And right now I have found following solution.

private Dialog[] aaa = new Dialog[2];

@Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DIALOG_1:
   aaa[0] = new AlertDialog.Builder(this)
   .setTitle(R.string.s_dlg1)
   .setPositiveButton(android.R.string.ok, this)
   .create();
   retrun aaa[0];

  case DIALOG_2:
   ...
   ...
  }

  return null;
 }

 @Override
 public void onClick(DialogInterface dialog, int whichButton) {
  if (dialog == aaa[0])) {
   ...
  }
  else if (dialog == aaa[1]){
   ...
  }
 }
0

This is an old question and still I've encountered the same problem xO. As a way around I've used the "hashCode" of the Dialog object. Depending on the dialog type you can cast the "Dialog Interface" to your specific type, and compare the "hashCode" of the "first" and "second" dialog vs the "hasCode" of the "DilaogInterface" object from the callback function.

"public void onClick(DialogInterface dialog, int whichButton) {}"

Function signature.

Andi Al
  • 21
  • 1
  • 4
0

this is working for me

 case Dialog_import_database:
            return new AlertDialog.Builder(Main.this)
            .setTitle(R.string.ImportDatabaseDialogTitle)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Log.i("Main","positive Button In dialog Box");

due what ever you wanna due when positive button is clicked here

                }
            })
            .setNegativeButton("Cancel", null)
            .setMessage(R.string.ImportDatabaseMessage)
            .create();
        }
  • You cannot access class variables if you declare it like this. – Monstieur Jan 25 '12 at 05:56
  • this code is placed in the ondialogcreate metode with a switch statement then you just call showdialog(Dialog_import_database); –  Jan 25 '12 at 13:12
  • @Matt Gibson You cannot access class variables from the inline onClick method. – Monstieur Apr 26 '12 at 03:20
  • @Kurian What error do you get when you try? Can you post your exact code from your attempt? I may be getting confused by your terminology, but, for example, this code is currently in my working app: `builder.setNeutralButton("C", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mSelectedRun = getNextRunNumber(); showRunClock(); }` where mSelectedRun is a member of my Activity class. – Matt Gibson Apr 26 '12 at 06:57
  • @MattGibson I cannot access mSelectedRun unless it's a static variable. – Monstieur Apr 26 '12 at 10:13
  • @Kurian What error do you get? (in my code, mSelectedRun is a normal, non-static member -- declaration is `private int mSelectedRun`. And honestly, this should work just fine. If you can't get it working, I'll try to post a full example later, but I'm at work at the moment...) – Matt Gibson Apr 26 '12 at 14:33
  • @MattGibson I don't have the SDK installed anymore, but I could only access static and final Activity class members from within an anonymous inner class. – Monstieur Apr 27 '12 at 03:59
-1

Is hard two identify directly which dialog has been showed instead not which button has been pressed, so if you populate the dialogs with different buttons you can do the trick. Something like this:

new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg1)
.setPositiveButton(android.R.string.ok, this)
.create();

new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg2)
.setNegativeButton(android.R.string.ok, this)
.create();

@Override
public void onClick(DialogInterface dialog, int whichButton) {
if (whichbutton == DialogInterface.BUTTON_POSITIVE) {
...
 }
else if (whichButton == DialogInterface.BUTTON_NEGATIVE){
 ...
 }
ibai
  • 11
  • 2