This question has already been answered:

How to get ArrayList with an Activity in a Fragment using the Parcelable interface and extract data in a Fragment?

Activity.class

public class MainActivity extends Activity { final String LOG_TAG = "myLogs"; ArrayList<Parcelable> parcelableArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList carList = new ArrayList(); carList.add(new Car(1, "Honda", "Black")); carList.add(new Car(2, "Toyota", "Blue")); carList.add(new Car(3, "Suzuki", "Green")); Bundle bundle=new Bundle(); bundle.putParcelableArrayList("name",carList); Fragment fragobj=new MyFragment(); fragobj.setArguments(bundle); getFragmentManager().beginTransaction() .replace(R.id.fmContent, fragobj) .commit(); } 

Fragragment.class

 public class MyFragment extends Fragment{ final String LOG_TAG = "myLogs"; Bundle bundle; TextView text; TextView article; ArrayList<String > arrayList = new ArrayList<String >(); ArrayList<Parcelable> parcelableArrayList; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); parcelableArrayList = getActivity().getIntent().getParcelableArrayListExtra("name"); if (parcelableArrayList !=null){ Log.d(LOG_TAG, " -------------------------> myBundle contain IS "+arrayList.get(1)); }else { final String LOG_TAG = "myLogs"; // Bundle bundle; Log.d(LOG_TAG, " -------------------------> myBundle contain IS "+"NULL"); } return view; } } 

In the class MyFragment.java, I get null on the line parcelableArrayList = getActivity (). GetIntent (). GetParcelableArrayListExtra ("Student");

How to organize the code for the transfer type ArrayList?

Reported as a duplicate by pavlofff android Jan 12 '17 at 15:46 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

    Here

    parcelableArrayList = getActivity (). getIntent (). getParcelableArrayListExtra ("name");

    you are trying to get data from the actvity intention, and you need from the fragment arguments. Those. So:

     parcelableArrayList = getArguments().getParcelableArrayList("name"); 
    • In the getArguments () method there is no this method for Fragment - Error: (67, 45) error: cannot find symbol getParcelableArrayListExtra (String) - mtb
    • @mtb, here is an extra word Extra. Updated the answer - YuriySPb ♦

    1. Make ArrayList a private field in your Activity.

    2. Create the following method in activity:

     piblic ArrayList getArrayList(){ return MyArrayList } 

    3. In the snippet, do the following:

     ArrayList<YourGeneric> MyNewArrayList = new ArrayList<>(); MyNewArrayList = MainActivity.getArrayList(); 
    • Your code will not compile because will not find such a method in the MainActivity, since it is not static, and you are trying to cause static. And in any case, do not need to. - Yuriy SPb ♦
    • Surprisingly works) - Martinez Toni
    • Apparently the variable MainActivity is declared above. If so, observe the variable naming conventions - your code in its current form does not read as it is executed. Variables should be called with a small letter - Yuriy SPb ♦