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.
Asked
Active
Viewed 3,139 times
2
-
https://developer.android.com/training/basics/fragments/communicating.html – fweigl Dec 28 '17 at 12:15
-
use bundles for that – Vivek Mishra Dec 28 '17 at 12:16
-
Use `interface` to communicating between fragments – Ramesh sambu Dec 28 '17 at 12:16
4 Answers
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");

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 CarDetailsFragment
class 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.