Forum Xamarin.Android

OnCreateDialog in SherlockDialogFragment not being called

JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

I'm extending a SherlockDialogFragment to show an alert dialog:

using Android.OS;
using Android.App;
using ActionBar_Sherlock.App;

namespace Journal.Droid
    public class AlertDialogFragment :SherlockDialogFragment
        string _Title;
        string _Message;

        public AlertDialogFragment (string title, string message)
            _Title = title;
            _Message = message;

        public override Dialog OnCreateDialog (Bundle savedInstanceState)

            if (savedInstanceState != null)
                _Title = savedInstanceState.GetString ("title");
                _Message = savedInstanceState.GetString ("message");

            var dlg = new AlertDialog.Builder (Activity);
            dlg.SetTitle (_Title);
            dlg.SetMessage (_Message);

            return dlg.Create ();

        public override void OnSaveInstanceState (Bundle outState)
            base.OnSaveInstanceState (outState);
            outState.PutString ("title", _Title);
            outState.PutString ("message", _Message);

And then calling it in a SherlockFragmentActivity like this:

    void btnExportClick (object sender, EventArgs e)

        System.Collections.ObjectModel.ReadOnlyCollection<Entry> Entries;
        List<Sighting> Sightings = new List<Sighting> ();

        if (editTextEmail.Text == "")
            AlertDialogFragment f = new AlertDialogFragment (Resources.GetString (Resource.String.error), Resources.GetString (Resource.String.emptyEmailAddress));
            f.Show (SupportFragmentManager, "error");

        Entries = Mobile.Application.Database.Entries;

        foreach (Entry Ent in Entries)
            Sightings.AddRange (Ent.Sightings);

        EmailExport.SendEmail (editTextEmail.Text, Sightings, Mobile.Application.Database.GetRecordFormatByName ("Journal"), Mobile.Application.Database, Mobile.Application.TaxonomicConfig, "en");
        Mobile.Application.UserOptions.EmailAddress = editTextEmail.Text;


The problem I'm having is that the f.Show (SupportFragmentManager, "error"); is not causing my AlertDialog to show. The constructor is beign called correctly, but OnCreateDialog is not being called, and the dialog does not appear.

Any thoughts as to what may be wrong?


  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Of course - I need to either return after the f.Show() (to allow the UI thread to pick up the f.Show), or run the creation of the AlertDialogFragment in RunOnUiThread.


Sign In or Register to comment.