How to pass data from an activity to a dialogFragment?

1

In my activity I have this:

mBtnSignUp.Click += (object sender, EventArgs e) => 
            {
                //Pull up dialog
                FragmentTransaction transaction = FragmentManager.BeginTransaction();
                dialog_SignUp signUpDialog = new dialog_SignUp();
                signUpDialog.Show(transaction, "dialog fragment");

                signUpDialog.mOnSignUpComplete += signUpDialog_mOnSignUpComplete;
            };
void signUpDialog_mOnSignUpComplete (object sender, OnSignUpEventArgs e)
        {

            Thread thread = new Thread (ActLikeARequest);
            thread.Start ();


        }
        private void ActLikeARequest()
        {

            Thread.Sleep (3000);
        }

And in Dialog I have:

public class OnSignUpEventArgs : EventArgs
    {
        private string mFirstName;
        private string mEmail;
        private string mPassword;

        public string FirstName
        {
            get{ return mFirstName; }
            set{ mFirstName=value;}

        }
        public string Email
        {
            get{ return mEmail; }
            set{ mEmail=value;}

        }
        public string Password
        {
            get{ return mPassword; }
            set{ mPassword=value;}

        }

        public OnSignUpEventArgs (string firstName, string email, string password) : base()
        {
            FirstName = firstName;
            Email = email;
            Password = password;
        }

    }

    class dialog_SignUp:DialogFragment
    {
        private Button mBtnSignUp;
        private EditText mFirstName;
        private EditText mEmail;
        private EditText mPassword;

        public event EventHandler<OnSignUpEventArgs> mOnSignUpComplete;


        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView (inflater, container, savedInstanceState);

            var view = inflater.Inflate (Resource.Layout.dialog_sign_up, container, false);

            mBtnSignUp = view.FindViewById<Button> (Resource.Id.btnDialogEmail);
            mFirstName = view.FindViewById<EditText> (Resource.Id.txtFirstName);
            mEmail = view.FindViewById<EditText> (Resource.Id.txtEmail);
            mPassword = view.FindViewById<EditText> (Resource.Id.txtPassword);

            mBtnSignUp.Click += mBtnSignUp_Click;



            return view;
        }

        void mBtnSignUp_Click (object sender, EventArgs e)
        {

            //User has clicked the sign up button

            mOnSignUpComplete.Invoke (this, new OnSignUpEventArgs(mFirstName.Text, mEmail.Text, mPassword.Text));
            this.Dismiss ();

        }
        public override void OnActivityCreated (Bundle savedInstanceState)
        {
            Dialog.Window.RequestFeature (WindowFeatures.NoTitle); //Sets the title bar to invisible
            base.OnActivityCreated (savedInstanceState);
            Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; //Set the animation
        }
    }

The question is, how do I pass string data to my Dialog , so when it is called, TextViews (mFirstName, mEmail, mPassword) get Text = *strings passadas* ?

    
asked by anonymous 27.04.2015 / 17:23

1 answer

0

You can have in your own class dialog_SignUp a constructor that receives these values:

class dialog_SignUp:DialogFragment {
    private string firstName;
    private string email;
    private string password;

    public dialog_SignUp(string pName, string pEmail, string pPassword) {
        firstName = pName;
        email = pEmail;
        password = pPassword;
    }
}

Within this class, in the OnCreateView method, assign to text fields:

mFirstName.setText(firstName);
mEmail.setText(email);
mPassword.setText(password);

And with that, the moment you instantiate this class you pass the 3 arguments to be filled in their due fields.

    
27.04.2015 / 18:09