1

I want to pass "ArrayList objArrayList" from one activity to second activity and want to receive there. I am creating a simple class in that there are four arraylist. I m creating object of that class and calling method of that class by passing parameters to be added in arraylist. After that I m adding that class object in objArrayList. HOw can I pass objArrayList from one activity to second activity and receive it there?

Thanks, Vishakha.

2 Answers2

2

First Context (can be Activity/Service etc)

You have a few options:

1) Use the Bundle from the Intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) Create a new Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);

3) Use the putExtra() shortcut method of the Intent

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);

New Context (can be Activity/Service etc)

Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
     String value = myIntent.getExtras().getString(key);
}

NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.

Your Arraylist class must be Parcelable or Serializable. So you should subclass it to add these functionalities as I think they miss from the original implementation.

Then look into these other questions they will help you. Serialization issue with SortedSet, Arrays, an Serializable

Community
  • 1
  • 1
Pentium10
  • 190,605
  • 114
  • 394
  • 474
0

Use the standard Intent mechanism. Put your list to extras of intent and pass it.

Vladimir Ivanov
  • 41,277
  • 17
  • 74
  • 99