8

I am trying to pass an ArrayList of Objects between multiple activities in my application. Is it possible to do this using an Intent using the setData() method?

Javacadabra
  • 5,417
  • 14
  • 77
  • 151

6 Answers6

12

If you want to send an ArrayList of objects then your class must implement the Parcelable or Serializable interface .

See these good tutorials for sending custom object between Activities

http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html

http://www.anddev.org/novice-tutorials-f8/simple-tutorial-passing-arraylist-across-activities-t9996.html

DLJ
  • 333
  • 2
  • 6
  • 19
ρяσѕρєя K
  • 130,641
  • 51
  • 193
  • 212
  • The first link had clear enough instructions and a working example. Thanks. – cavpollo Nov 26 '14 at 23:20
  • Guys , I have problem I'm getting **TransactionTooLargeException** when I pass large Arraylist Using Parcelable or Serializable . Need help on that. – Bhavin Patel May 13 '17 at 06:45
6

Use below code for pass arraylist in intent.

Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putParcelableArrayListExtra("Data", mArraylist);
startActivity(mIntent);

Use below code For get arraylist from Intent.

Bundle bdl = getIntent().getExtras();
mArraylist1 = bdl.getParcelableArrayList("Data");
Dipak Keshariya
  • 22,009
  • 18
  • 75
  • 127
3

First you need to extend parcelable class in your Object class. Then you can pass it through intent via intent.putParcelableArrayListExtra("PASSING_DATA", data);

here data is arraylist of custom objects.

see Parcelable and Parcelable Tutorial for better undertanding

keyser
  • 18,349
  • 16
  • 58
  • 97
AAnkit
  • 26,788
  • 11
  • 57
  • 70
0

yes, it is possible... You need to implement Serializable class by your object class.

Sushrita
  • 705
  • 10
  • 29
Richa
  • 3,157
  • 1
  • 20
  • 25
0

If the objects implement Parcelable you can use the putParcelableArrayList method like this:

Bundle data = new Bundle();
data.putParcelableArrayList("myArrayList", myList);
Intent i = new Intent();
i.putExtra("data", data);

Hope that helps.

Adrián Rodríguez
  • 1,851
  • 14
  • 16
0

You can make the arraylist static where you are defining it, and only pass the position to the next activity

chinmay2312
  • 93
  • 1
  • 2
  • 8