2

i'm pretty much stuck. I want to pass user input from the fields below from one fragment, CarDetailsFragment to another fragment ConfirmationFragment. I also want to pass the states of the checkboxes(whether checked or unchecked). Kindly help.

enter image description here

Fiona Muthoni
  • 85
  • 2
  • 5

4 Answers4

0

Use POJO class for this..

On Sending...

 YourNewFragment ldf = new YourNewFragment ();
 Bundle args = new Bundle();
 args.putExtra("DATA", POJO_MODEL);
 ldf.setArguments(args);

//Inflate the fragment getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

On Receiving...

POJO_MODEL pojo_model = POJO_MODEL_CAST)getArguments().getSerializable("DATA");
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
Nikhil Borad
  • 2,065
  • 16
  • 20
0

Use Bundle to send String:

//Put the value

   YourNewFragment ldf = new YourNewFragment ();
   Bundle args = new Bundle();
   args.putString("YourKey1", "YourValue");
   args.putString("YourKey2", "YourValue");
   ldf.setArguments(args);

//Inflate the fragment

getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

In onCreateView of the new Fragment:

//Retrieve the value

String value = getArguments().getString("YourKey");

Duplicate

Subin Babu
  • 1,515
  • 2
  • 24
  • 50
0

Send data with Bundle as:

Bundle bundle = new Bundle();
bundle.putString("key", "value");
// set Fragmentclass Arguments
CarDetailsFragment carDetailsFragment = new CarDetailsFragment();
carDetailsFragment.setArguments(bundle);

//replace fragment  
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, carDetailsFragment);
transaction.addToBackStack(null);
transaction.commit();

and in CarDetailsFragmentclass onCreateView() method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String value  = getArguments().getString("key");    
    return inflater.inflate(R.layout.fragment, container, false);
}
AGM Tazim
  • 2,213
  • 3
  • 16
  • 25
-1

What I would recommend is to use a library like EventBus.

It's very lightweight and will make your life much easier. Please check the page I have linked above.

All you would need to do post an event and you create subscribe to those events in the whole app.

halfer
  • 19,824
  • 17
  • 99
  • 186
Atiq
  • 14,435
  • 6
  • 54
  • 69