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 c开发者_开发问答lass 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
.
You can make your ArrayList as public static and can access anywhere you want by calling it as Activity_name.array_list_name.
Where Activity_name is the class name where you will define ArrayList.
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.
passing Object is not possible without making it extend Parcelable
.. So what i suggest is create class called store()
or something and make it a singleTon
. which has a private field of type ArrayList()
. and create getters and setters for the same variable and where ever you are creating object just say store().getInstance().setObject(yourObject);
and while getting
store().getInstance().getObject();
精彩评论