1

I try to 'develop' an Android application with C# (VS2010 + dot42/mono) which shows lots od messages, gets users' input, and creates AlertDialogs. The questions are: 1. Do you know how to catch OnCancel Event returned from Android AlertDialog(s)? 2. How to detect which exactly AlertDialog sent it?

To be more clear, I am trying to get the AlertDialog.Builder SetOnCancelListener(IDialogInterface_IOnCancelListener onCancelListener) to work.

I have created a method which displays a simple message box, and I try to catch somehow (but it is all wrong so far) the onCancel event. Please see below. Can you help me?

private void button2_OnClick(object sender, EventArgs eventArgs)
    {
        AlertDialog.Builder a_builder = new AlertDialog.Builder(this);

        a_builder.SetMessage("Is this all?");
        a_builder.SetTitle("Question");
        a_builder.SetPositiveButton("Yes", OnMsgClick_Result2 );
        a_builder.SetNegativeButton("Not yet" OnMsgClick_Result2);
        a_builder.SetCancelable(true);
        a_builder.SetOnCancelListener(
                  new IDialogInterface_IOnCancelListener(
                  new IDialogInterface_IOnClickListener(IDialogInterface dialog) { 
                  switch (dialog.which) //<--------- ???
                     {
                     case _dialogA:
                     text1.settext("DialogA was canceled");
                     break;

                     case _dialogB:
                     text1.settext("DialogB was canceled");
                     break;

                     default:
                     text1.settext("Nothing has been canceled");
                     break;
            }
           })
           ); //<--- ??? ERROR HERE of course 

       a_builder.Create().Show();
    } 
TomeeNS
  • 455
  • 4
  • 10

1 Answers1

1

You can only detect the dialog itself being closed. So for each dialog you create with AlertDialog.Builder you will need to have a seperate CancelListener.

Edit:

Something like this would do:

var builder = new AlertDialog.Builder(this);
builder.SetTitle("Herp");
builder.SetPositiveButton("Derp", (sender, args) => { /* do stuff on OK */ });
builder.SetNegativeButton("Durr", (sender, args) => { /* do stuff on No */ });
builder.SetCancelable(true);
builder.SetOnCancelListener(new MyCancelListener());
builder.Show();

public class MyCancelListener 
    : Java.Lang.Object, IDialogInterfaceOnCancelListener
{
    public MyCancelListener(/* you could pass stuff here */)
    {

    }

    public void OnCancel(IDialogInterface dialog)
    {
        //Do stuff when cancelled
        //Maybe with stuff from ctor
    }
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • I see. Do you know how to implement several CancelListeners in this case? – TomeeNS Sep 15 '13 at 21:44
  • Could you show me a working example, because I got no idea how to do that. Please :) I've foud that in order to make it work we have to attach the **OnCancelListener** Interface to our class like this?: `public class MainActivity : Activity, IDialogInterface_IOnCancelListener { //... //and then: public void OnCancel(IDialogInterface dialog) { //taadaamm, but how to get some id to know, //which Dialog is it? Toast.MakeText(this_context,"There was a dialog X canceled.",5000).show(); } //... }` – TomeeNS Sep 16 '13 at 18:19