How to pass data from an activity to a dialogFragment?

Asked

Viewed 135 times

1

In my activity I have it:

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 the Dialog 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 data from my activity (strings) to mine Dialog, so that when this is called, the TextViews (mFirstName, mEmail, mPassword) stay with the Text = *strings passadas*?

  • I think within your very class dialog_SignUp you can create a constructor of it that takes 3 arguments, which in OnCreateView you will use to fill in these fields. You have already made this attempt?

  • @Cesarmiguel now that said, tried and resulted Thank you! Post as a response to gain reputation. Hug

  • @Ruiquaresma, I didn’t answer :P

  • @Cesarmiguel answered the comment ;)

1 answer

0


You can have in your own class dialog_SignUp a builder 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 method OnCreateView, 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 its proper fields.

  • that’s right, that’s enough, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.