Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

What is the best way to pass objects or an array of objects with intents?

When it comes to passing Strings or primitive data with intents using the putExtra and getExtra menthods, that method is pretty straightforward. I've been working on a project where I needed to pass an array of objects to the next activity.

At first I implemented Serializable on the class but that method seemed like a band-aid, is not recommended, and actually didn't work on one device when it worked on another. Through some trial and error, and a bit of reading, I think I finally understand how to do so correctly by implementing the Parcelable interface on my class.

This seems like something important and very common so I wanted to share my resources. I used the following three posts from StackOverflow to guide me to my current solution...

http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents http://stackoverflow.com/questions/17629224/passing-object-array-using-parcelable-object http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcelable-interface

If anyone has a better way of doing this or some pointers please let me know.

Thanks for the list, I've been struggling with this same issue. I don't understand why it's so complex a process when it seems like such a common need.

1 Answer

Hey Jeremiah!

There is no "best way" to do things in programming - it all depends on what you are doing. So, a "best method" for fixing one problem may not be the best method for another.

There's usually a lot of things on the internet like you've seen about different methods and, it's up to you which one works best as, you know what they're going to be used for :)

I understand that, but that answer isn't really too helpful. Perhaps I should have asked "What are some alternative ways to...". I was looking for the most commonly accepted way a beginner could accomplish that task.

Off the top of my head, there's only two alternate methods I can think of.


One would be to pass in a variable through a method. Like This:

DOC1.java

String helloWorld = "Hello World!";
DOC2.sendString(helloWorld);

DOC2.java

public static sendString(String myString) {
Log.d(TAG, myString);
}

Another would be to declare a variable public or protected so that it can be used elsewhere:

DOC1.java

public static String helloWorld = "Hello World!";

DOC2.java

Log.d(TAG, DOC1.helloWorld);

Hopefully this should help but, give me a shout if you need anything else :)