2

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.

ColdFire
  • 6,764
  • 6
  • 35
  • 51

3 Answers3

1

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • thanks for answering..setting the arrayList as public may be a solution but i'd like send it to the next activity(which will change it and those changes need to be kept only in this activity) so i need to know how to make my class parcelable – ColdFire Aug 10 '11 at 13:44
  • No it won't change the values in the ArrayList if you define it public static. – Lalit Poptani Aug 10 '11 at 14:03
  • actually change made to a **static** object will be made all over the application which won't be helpfull.. – ColdFire Aug 10 '11 at 14:42
  • Else you can create a class that extends Application and work accordingly. – Lalit Poptani Aug 10 '11 at 15:37
0

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();
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • thanks for the suggestion but i was woundering if any one knew how to implement Parcelable with a drawable in it. – ColdFire Aug 10 '11 at 14:43
0

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.

Josh
  • 10,618
  • 2
  • 32
  • 36
  • thank you for your reply, first the list doesn't include only drawables and the reason for passing such list is that the first activity has a form and will make a call to a web service after making some reseach. the result includes some image url and will be loaded in a listview and in the activity that will be started next. so i guessed sending the drawable would be better then load them twice from the web.. isn't it? i hope i made myself clear, – ColdFire Aug 13 '11 at 09:33
  • Alright, then let me ask you this - what happens in your first activity when you rotate the device and go through the destroy/create cycle? Or what if you press Home, then go back to your app using a long-press on the Home button? Either your images are downloading again, or you crash. You need to save those downloaded images in a temporary cache on disk, so that you can access them again without downloading them. Once you do this, it should be trivial to access them from the next activity as well. – Josh Aug 18 '11 at 17:15