简体   繁体   中英

Passing object or Array List from one Fragment to another Fragment using Android Studio

I am trying to pass the object or the arrayList from this fragment to another. I was using the Intent method but I had the same problem which I do not know what parameter should I put in bundle.putParcelable() method.

BlankFragment1 sendData = new BlankFragment1();         
final Series s = new Series(itemName.getText().toString(),            
Integer.parseInt(itemYear.getText().toString())                 
,Integer.parseInt(itemNumberofSeas.getText().toString()),     
dropDown.getSelectedItem().toString());          
Bundle bundle = new Bundle();         
bundle.putParcelable(s);         
sendData.setArguments(bundle);

You can see below example

Fragment one

android.support.v4.app.FragmentTransaction ft = 
    getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
OfficeCategoryFragment frag = new OfficeCategoryFragment();

Bundle bundles = new Bundle();
Division aDivision = divisionList.get(position);

// ensure your object has not null
if (aDivision != null) {
    bundles.putSerializable("aDivision", aDivision);
    Log.e("aDivision", "is valid");
} else {
    Log.e("aDivision", "is null");
}
frag.setArguments(bundles);
ft.replace(android.R.id.content, frag);
ft.addToBackStack(null);
ft.commit();

fragment 2

Bundle bundle = getArguments();
Division division= (Division) bundle.getSerializable("aDivision");
Log.e("division TEST", "" + division.getName());

Use setArguments to add the data same as putextra in activity.

Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
bundles.putSerializable("KEY_ARRAYLIST", DIVID);
bundle.putString("longitude", longitude);
bundle.putString("board_id", board_id);
MapFragment mapFragment = new MapFragment();
mapFragment.setArguments(bundle);

And to get data use getArguments same as getExtra in Activity

String latitude =  getArguments().getString("latitude")
 Arraylist obj=    getArguments().getSerializable("KEY_ARRAYLIST");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM