9

I'm trying to pass an ArrayList from my first Activity to the next one. Basically, the first activity parses an XML file and creates an ArrayList with objects inside. What I want to do is send that ArrayList to my second activity and show some of the object data in a ListView.

I thought of doing this with an intent, but it looks like only primitive datatypes are usually passed through intents. Is this right?

If so, what would be a better solution to pass the data? Certainly Android must provide something to be able to do this kind of thing.

Any help/code examples are really appreciated..

Thanks

EDIT:

I solved this by first creating and calling the intent, and only parsing the XML in the Activity that I called. This way I didn't need to pass the objects anymore. But for those interested, you can read about how to pass data through activities, here.

6 Answers6

5

Complex types may be passed by means of Parcelable. An example is in this question:

Help with passing ArrayList and parcelable Activity

Community
  • 1
  • 1
NickT
  • 23,844
  • 11
  • 78
  • 121
2

I would do a toString on the array and pass it as an extra by doing a intent.putExtra("label". array.toString()); and then just recover it in the new activity.

azharb
  • 979
  • 6
  • 15
  • But will this work with complex objects as well, and is it performant? –  Apr 29 '11 at 18:13
  • If performance is your priority, I would not recommend it. I used it to pass a giant xml from one activity to another and it caused a major lag. Ended up fetching the data in the second activity instead. For smaller objects though, I feel like it should be okay. – azharb Apr 29 '11 at 18:16
  • 1
    `Intent.putExtra()` takes in a Bundle though, so you would need to do something more like this: `Bundle bundle = new Bundle(); bundle.putStringArray("arrayLabel",(String[])arrayList.toArray()); intent.putExtra("extrasBundle",bundle);` Then inside the Activity that will receive the ArrayList: `Bundle bundle = intent.getBundleExtra("extrasBundle"); ArrayList list = Arrays.asList(bundle.getStringArray("arrayLabel"));` – BigFwoosh Apr 29 '11 at 18:19
1

You can pass a String List using putStringArrayListExtra(String name, ArrayList<String> value) if it is strings. Or, you could serialize List and then use putExtra(String name, Serializable value).

If you don't want to/can't use the above, you could use a central util class with a static reference to the List. Just set it in your first Activity and get it in the second.

Haphazard
  • 10,900
  • 6
  • 43
  • 55
  • I know about the putStringArrayListExtra() method, but I need this for more complex objects. And using static seems like a bad practice to me, looks really messy. –  Apr 29 '11 at 18:26
  • Using a static reference is of questionable repute, sure, but you can keep it very clean by using a dedicated class to get/set/store it. I personally use it in an Android app with multiple pages and couldn't be happier. – Haphazard Apr 29 '11 at 18:29
1

This could be totally bad practice and I wouldn't know any better, but you could declare the ArrayList as public and static. Then just access it with Activity.ArraylistName.

Amplify91
  • 2,690
  • 5
  • 24
  • 32
  • 1
    This is being recommended a lot, but it looks really messy though. –  Apr 29 '11 at 18:25
  • In my application I create the ArrayList in the same class as the objects, I store inside of it (in my case, a custom View). This makes it fairly easy to keep track of and stay organized. – Amplify91 Apr 29 '11 at 18:42
0

I made this trick to send from first Activity to second.

The first activity

ArrayList<String> mylist = new ArrayList<String>();  
Intent intent = new Intent(ActivityName.this, Second.class);
intent.putStringArrayListExtra("key", mylist);
startActivity(intent);

The second activity

To retrieve

ArrayList<String> list =  getIntent().getStringArrayListExtra("key");
LifeInstructor
  • 1,622
  • 1
  • 20
  • 24
0

You can set the scope of ArrayList at application level or you can do this using parcelable.

Nishant Shah
  • 3,442
  • 5
  • 25
  • 34