0

I have two fragment, one is Fragment X and Fragment y.. Fragment X is used for signup process using webservice. I want to pass(email,mobile) same value from Fragment X to Fragment Y.

Here my code:

Fragment x:

Signup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String sName = Name.getText().toString();
                    String sPhoneNo = Phone.getText().toString();
                    String sEmailId = EmailId.getText().toString();



                    // Instantiate Http Request Param Object
                    RequestParams params = new RequestParams();

                    if (Utility.isNotNull(sName) && Utility.isNotNull(sPhoneNo) && Utility.isNotNull(sEmailId) ) {
                        if (Utility.line2_validate(sName)) {
                            if (Utility.validate(sPhoneNo)) {
                                if (Utility.email_validate(sEmailId)) {

                                   //Get value from this Fragment 
                                    FragmentY pf = new FragmentY ();
                                    Bundle args = new Bundle();
                                    args.putString("mobile", sPhoneNo);
                                    args.putString("email",sEmailId);
                                    pf.setArguments(args);

                                    //Inflate the fragment
                                    getFragmentManager().beginTransaction().add(R.id.passwordLayout, pf).commit();

                                    // Put Http parameter username with value of Name Edit View control
                                    params.put("name", sName);
                                    // Put Http parameter email with value of Email Edit Value control
                                    params.put("email", sEmailId);
                                    // Put Http parameter mobile with value of Mobile Edit Value control
                                    params.put("mobile", sPhoneNo);
                                    // Invoke Signup RESTful Web Service with Http parameters
                                    signUpWS(params);

                                    //Toast.makeText(getActivity().getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                                    //((MainActivity) getActivity()).navigatetoPasswordActivity();

                                }
                                else {
                                    EmailId.setError("Enter valid Email");
                                }

                            }
                            else {
                                Phone.setError("Enter valid Mobile");
                            }

                        }
                        else {
                            Name.setError("Enter valid Name");
                        }

                    }
                    else {
                        Toast.makeText(getActivity().getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_SHORT).show();
                    }
                }



            });

Fragment Y:

//  Retrive the value from fragment x
String strMobile = getArguments().getString("mobile");
                String strEmail = getArguments().getString("email");

Please anyone help me..

Thanks in advance!

Razul
  • 99
  • 1
  • 12
  • I don't get the problem, you have your Fragment-Object as `pf`. Why don't you simply set 2 parameters on that object? – JohnnyAW Aug 13 '15 at 10:48
  • I dont know what to do.. Help me – Razul Aug 13 '15 at 10:50
  • http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments http://stackoverflow.com/questions/13445594/data-sharing-between-fragments-and-activity-in-android http://developer.android.com/training/basics/fragments/communicating.html http://stackoverflow.com/questions/25062879/sharing-data-between-android-fragments – Andrew Fielden Aug 13 '15 at 10:55
  • http://stackoverflow.com/questions/24081101/using-localbroadcastmanager-to-communicate-from-fragment-to-activity/24083101#24083101 – Umang Kothari Aug 13 '15 at 12:09

3 Answers3

1

You can pass value to fargment by creating constructor in fragment

For Example,

for fragment x pass value in constructor

PasswordFragment pf = new PasswordFragment (name, email, mobile);

for fragmnet y you can retrive value from this constructor

String name, email, mobile
public PasswordFragment(String name, String email, String mobile) {
        // TODO Auto-generated constructor stub
        this.name=name;
        this.email=email;
        this.mobile=mobile;
}
Jenisha Makadiya
  • 832
  • 5
  • 17
0

I know that Fragment should communicate through the Activity they are attached to; a way to solve your problem would be to declare

final Activity activity = this.getActivity();

and use it to pass the values you need via Activity attributes (private or public that's your choice), like

String value1;
String value2; //Into Activity

than, once you filled them up, you can retrieve their value from FragmentY.

EDIT: of course, you will need a cast to YourCustomActivity when declaring the final Activity object in your Fragment.

StG
  • 257
  • 2
  • 11
0

I would advise you to look into this. http://developer.android.com/training/basics/fragments/communicating.html

Its pretty clean and advisable.No fragments should communicate themselves directly.best approach is to use the calling activity as a mediator between both the fragments

Huzefa Gadi
  • 1,129
  • 1
  • 7
  • 11