Passing ArrayList through Intent

The question:

I am trying to pass an arrayList to another activity using intents. Here is the code in the first activity.

        Toast.makeText(this, "edit was clicked", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this, editList.class);
        intent.putStringArrayListExtra("stock_list", stock_list);

This is where I try to retrieve the list in the second activity. Is something wrong here?

Intent i = new Intent(); //This should be getIntent();
    stock_list = new ArrayList<String>();

    stock_list = i.getStringArrayListExtra("stock_list");

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

In your receiving intent you need to do:

Intent i = getIntent();  
stock_list = i.getStringArrayListExtra("stock_list");

The way you have it you’ve just created a new empty intent without any extras.

If you only have a single extra you can condense this down to:

stock_list = getIntent().getStringArrayListExtra("stock_list");

Method 2

I have done this one by Passing ArrayList in form of String.

  1. Add compile '' in dependencies block build.gradle.

  2. Click on Sync Project with Gradle Files

public class Cars {
    public String id, name;

When you want to pass ArrayList:

List<Cars> cars= new ArrayList<Cars>();
cars.add(getCarModel("1", "A"));
cars.add(getCarModel("2", "B"));
cars.add(getCarModel("3", "C"));
cars.add(getCarModel("4", "D"));

Gson gson = new Gson();

String jsonCars = gson.toJson(cars);

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("list_as_string", jsonCars);

Get CarsModel by Function:

private Cars getCarModel(String id, String name){
       Cars cars = new Cars(); = id; = name;
    return cars;

You have to import java.lang.reflect.Type ;

on onCreate() to retrieve ArrayList:

String carListAsString = getIntent().getStringExtra("list_as_string");

Gson gson = new Gson();
Type type = new TypeToken<List<Cars>>(){}.getType();
List<Cars> carsList = gson.fromJson(carListAsString, type);
for (Cars cars : carsList){
   Log.i("Car Data","-";

Hope this will save time, I saved it.


Method 3

if you using Generic Array List with Class instead of specific type like


private ArrayList<Model> aListModel = new ArrayList<Model>();

Here, Model = Class

Receiving Intent Like :

aListModel = (ArrayList<Model>) getIntent().getSerializableExtra(KEY);


Here Model-class must be implemented like:
ModelClass implements Serializable

Method 4

Suppose you need to pass an arraylist of following class from current activity to next activity
// class of the objects those in the arraylist
// remember to implement the class from Serializable interface
// Serializable means it converts the object into stream of bytes and helps to transfer that object

public class Question implements Serializable {

in your current activity you probably have an ArrayList as follows

ArrayList<Question> qsList = new ArrayList<>();
qsList.add(new Question(1));
qsList.add(new Question(2));
qsList.add(new Question(3));

// intialize Bundle instance
Bundle b = new Bundle();
// putting questions list into the bundle .. as key value pair.
// so you can retrieve the arrayList with this key
b.putSerializable("questions", (Serializable) qsList);
Intent i = new Intent(CurrentActivity.this, NextActivity.class);

in order to get the arraylist within the next activity

//get the bundle
Bundle b = getIntent().getExtras();
//getting the arraylist from the key
ArrayList<Question> q = (ArrayList<Question>) b.getSerializable("questions");

Method 5

//arraylist/Pojo you can Pass using bundle  like this 
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle args = new Bundle();

Get SecondActivity like this
  Intent intent = getIntent();
        Bundle args = intent.getBundleExtra("BUNDLE");
String filter = bundle.getString("imageSliders");

//Happy coding

Method 6

    public class StructMain implements Serializable {
    public  int id;
    public String name;
    public String lastName;

this my item . implement Serializable
and create ArrayList

ArrayList<StructMain> items =new ArrayList<>();

and put in Bundle

Bundle bundle=new Bundle();

and create a new Intent that put Bundle to Intent

Intent intent=new Intent(ActivityOne.this,ActivityTwo.class);

for receive bundle insert this code

Bundle bundle = getIntent().getExtras();
ArrayList<StructMain> item = (ArrayList<StructMain>) bundle.getSerializable("test");

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment