I know you can pass in an array list of String via intent, but what if it’s an array list of some object that I defined? Say an array list of Bicyles, how do I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can make your objects implement Parcelable and use
putParcelableArrayListExtra. Alternatively, you can serialize your objects in some way and put the byte array of your serialized objects. However, whileSerializableis easier to implement, it does not perform as well at run time–requiring all objects to be converted to byte streams and generating lots of temporary objects.There are a lot of examples of how to implement
Parcelable, including several in this thread. If you’re using Kotlin, the easiest is to use thekotlin-parcelizeplugin. First, add the following to your module’s build.gradle file:Then, make your
Bicycleclass parcelable:Now you can pass an array list of
Bicyclevia an intent usingputParcelableArrayListExtra:The receiving activity can retrieve the bicycles like this:
If you’re targeting API level 33 or later, you can use:
which has better type safety.