Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Return data from dialog fragment to parent activity

MosiMosi INMember

I use the following code to open a dialog fragment in which the user enter some text

var transaction = FragmentManager.BeginTransaction(); var dialogFragment = new userDataModal(); dialogFragment.Show(transaction, "userDataModal");

There's an edit text element in the dialog fragment, how can retrieve the value of this edit text in my activity?

Posts

  • alexander1113alexander1113 USMember ✭✭

    This worked excellent for me, except I didn't use the DialogEventArgs, because I didn't need to pass anything back to the activity. I used Lori's example and did the following:

    In my dialog:
    public event EventHandler DialogClosed;

    overriding OnDismiss:

    public override void OnDismiss(IDialogInterface dialog)
    {
    base.OnDismiss(dialog);
    if (DialogClosed != null)
    {
    DialogClosed(this, null);
    }
    }

    In my Activity:
    dialogQuantity.DialogClosed += DialogQuantity_DialogClosed;

    private void DialogQuantity_DialogClosed(object sender, EventArgs e)
    {
    //stuff to do
    }

  • LoiudenvierLoiudenvier BRMember

    The "EventHandler" pattern won't work if your dialog maintain state on screen rotations. The activity will get destroyed, and the eventhandler will be calling on a dangling activity. I think the best approach is to use the well tested pattern of creating an interface, implement it on the calling activity and get a reference to it on the OnAttach method override.

Sign In or Register to comment.