EaranMaleasi
EaranMaleasi

Reputation: 925

How to dismiss a Alert Dialog in Mono for android correctly?

In my application i have a Custom AlertView, which works quite good so far. I can open it the first time, do, what i want to do, and then close it. If i want to open it again, i'll get

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

so, here some code:

public Class ReadingTab
{
    ...
    private AlertDialog AD;
    ...
    protected override void OnCreate(Bundle bundle)
    {
         btnAdd.Click += delegate
         {
             if (IsNewTask)
             {
                 ...
                 AlertDialog.Builer adb = new AlertDialog.Builer(this);
                 ...
                 View view = LayoutInflater.Inflate(Resource.Layout.AlertDView15ET15TVvert, null);
                 adb.setView(view)
              }
              AD = adb.show();
         }
     }
 }         

that would be the rough look of my code. Inside of btnAdd are two more buttons, and within one of them (btnSafe) i do AD.Dismiss() to close the Alert dialoge, adb.dispose() hasn't done anything. the first time works fine, but when i call it the secon time, the debugger holds at AD = adb.show(); with the Exception mentioned above.

So what do i have to do, to remove the Dialoge from the parent? i can't find removeView() anywhere.

Upvotes: 1

Views: 1479

Answers (1)

my code smells
my code smells

Reputation: 461

If you are setting up an AlertView once and then using it in multiple places (especially if you are using the same AlertView across different Activities) then you should consider creating a static AlertDialog class that you can then call from all over the place, passing in the current context as a parameter each time you want to show it. Then when a button is clicked you can simply dismiss the dialog and set the instance to null. Here is a basic example:

internal static class CustomAlertDialog
{
    private static AlertDialog _instance;
    private const string CANCEL = @"Cancel";
    private const string OK = @"OK";
    private static EventHandler _handler;

    // Static method that creates your dialog instance with the given title, message, and context
    public static void Show(string title,
        string message,
        Context context)
    {
        if (_instance != null)
        {
            throw new Exception(@"Cannot have more than one confirmation dialog at once.");
        }

        var builder = new AlertDialog.Builder(context);
        builder.SetTitle(title);
        builder.SetMessage(message);

        // Set buttons and handle clicks
        builder.SetPositiveButton(OK, delegate { /* some action here */ });
        builder.SetNegativeButton(CANCEL, delegate { /* some action here */});

        // Create a dialog from the builder and show it
        _instance = builder.Create();
        _instance.SetCancelable(false);
        _instance.Show();
    }
}

And from your Activity you would call your CustomAlertDialog like this:

CustomAlertDialog.Show(@"This is my title", @"This is my message", this);

Upvotes: 4

Related Questions