Search code examples
androidandroid-intentdrawableparcelable

How to pass ArrayList<my_custom_class> to an other activity?


This question has 2 parts

I'm trying to pass an ArrayList from one Activity to another. this Arraylist contains some objects instantiated from a class that I've created(contain some strings and a Drawable).

I found online that i need to make my class Parcelable which was a problem especially with a Drawable.(this is the first part)

Once my class implements Parcelable, how would i be able to send/get my Arraylist.


Solution

  • I strongly suggest you reevaluate your architecture. You shouldn't be passing drawables across activity boundries for several reasons, including context leaking, bitmap leaking, and potentially breaking the size limit on IPC calls.

    If you really need this behavior, the proper approach would be to save off your drawable data in a database/SharedPreference, and your bitmaps to files, then when the second activity starts, you can recreate those drawables by pulling in the data and images.

    Could you describe why two different activities need access to the same list of drawables? I suspect some restructuring could avoid the issue entirely.