Skip to content Skip to sidebar Skip to footer

How To Get Value From One Fragment And Pass To Another Fragment

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

Solution 1:

You can pass value to fargment by creating constructor in fragment

For Example,

for fragment x pass value in constructor

PasswordFragmentpf=newPasswordFragment (name, email, mobile);

for fragmnet y you can retrive value from this constructor

String name, email, mobile
publicPasswordFragment(String name, String email, String mobile) {
        // TODO Auto-generated constructor stubthis.name=name;
        this.email=email;
        this.mobile=mobile;
}

Solution 2:

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

finalActivityactivity=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.

Solution 3:

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

Post a Comment for "How To Get Value From One Fragment And Pass To Another Fragment"