0

I have a "User" object and it has properties like name, email, phone number, address etc.

Now I have an activity called "Register_Activity" which has different fragments for setting name, validating and setting email, etc.
Now in the SetNameFragment, I create an instance of the User object and call setName on it.

Now how to I pass this object to the next fragment? I was thinking of using ViewModel but I don't think it's meant for such operations.
Another way which I have been using is creating an instance of User in Activity and then calling the object from the fragment and setting its values, It works but I want to be sure if this is the right way.

Zun
  • 1,553
  • 3
  • 15
  • 26
Shivam Dhuria
  • 35
  • 1
  • 7

3 Answers3

0

You can have the User object inside your activity

public class MainActivity extends AppCompatActivity{

User userInfo;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        UserInfo = new User();
}
}

And inside Fragments you can access that object and set its values

public class Fragment1 extends Fragment {
@Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment1);

    ((MainActivity)getActivity()).userInfo.setUserName("UserName")
    .....
    .....
    return view;
    }
}


public class Fragment2 extends Fragment {
@Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment2);

    ((MainActivity)getActivity()).userInfo.setUserEmail("UserEmail")
    // set Other values
    .....
    .....
    return view;
    }
}
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
  • The only downside of this approach is the tight coupling between MainActivity and Fragment. Better to use an interface as a way to communicate between activity and fragment – Zun Feb 15 '19 at 09:46
  • There are other methods you can use. You can pass data using bundle or by static fragment instance method and the one you mentioned. – Abid Khan Feb 15 '19 at 10:22
  • And for the concern of want to more secure as you mentioned in post the bundle option is batter in that case. – Abid Khan Feb 15 '19 at 10:25
0

you can use bundle class to send data from one frgment to another... First Fragment step 1 : create Bundle class like,

Bundle bundle = new Bundle();
bundleFirstFragment.putString("name","John snow");

step 2: attach your bundle of data to the fragment like,

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    Fragment secondFragment= new SecondFragment();
    secondFragment.setArguments(bundle );
    transaction.replace(R.id.container, secondFragment);
    transaction.addToBackStack(null);
    transaction.commit();

Second Fragment..

Bundle bundle = getArguments();
String name = bundle.getString("name")
Log.v("name value :"+name);
Dinesh Sarma
  • 461
  • 3
  • 14
0

You can provide name value to activity (please dont do it directly. Make activity which implement some interface with callback methods and set it to fragment as interface object), and then provide name value to next fragment using arguments.

Also, do you really need to use different fragments? Maybe you can just use different views in one activity and swap them when you needed. To achieve this logic you can use viewpager or viewflipper

Darthoo
  • 301
  • 1
  • 2
  • 14