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

Michael B
Michael B
2,997 Points

Passing serialized custom data types using intents

Is it possible to pass custom data types from one activity to another? And if it is, how do I unload the data on the receiving side?

For example, I created a data type School[] and implemented serializable. Now, I think I am able to pass the School[] data from my MainActivity to the other activity, but I don't know how to unload it. Usually, we have to specify the data type using the "intent.get..." commands (for example, getDoubleExtra or getStringArrayList), but since I'm using a custom data type, I'm not sure which command to use. Any suggestions?

2 Answers

Michael B
Michael B
2,997 Points

For all those people looking for an answer to this, I found an example online that solved my problem. To pass a custom serialized data type (in my case, "School") using an intent, you would

Intent intent = getIntent();
School detail = (School) intent.getSerializableExtra(Name_of_Intent_Data);
Seth Kroger
Seth Kroger
56,413 Points

For a custom type you can (implement the Parcelable interface in your custom type and use getParcelableExtra() or getParcelableArrayExtra() to get the data out. There's an example doing that in the Android Lists and Adapters course.

There are also 3rd-party libraries you can use for passing data between components like EventBus and RxJava (Treehouse workshop). EventBus is low impact and learning curve if you just want to pass data. RxJava is a steeper learning curve, but does a lot more.

Michael B
Michael B
2,997 Points

Hi Seth. Thanks for answering! Just a question though, is there no way I could do the same operation while implementing a Serialize interface? Or is there a reason why Parcelable is more recommended in this case?