0

I have an Object that I need to be able to pass between Activites. It implements Parcelable and I've written all the code related to that.

The problem is that one of the properties of the Object is a Drawable - and really needs to be. Unfortunately, Drawable is neither Parcelable or Serializable. I don't understand how to pass it.

The reason for having the Drawable is that I need to cache an Image that I've downloaded from the internet at runtime. I don't want to cache the images on the filesystem, since this would potentially end up using up a lot of space over time.

I'm putting the image into a Drawable so that I can easily put it into an ImageView.

Any Ideas?

Paul Hunnisett
  • 898
  • 1
  • 17
  • 36

3 Answers3

1

You can store your unParcelable data in a custom ContentProvider,then pass the uri references to it.

oznus
  • 2,446
  • 2
  • 25
  • 18
1

in your Application:

HashMap<String,Object> tempObjects = new H....

public Object getTempObject(String key) {
  Object o = null;
  o = tempObjects.get(key);
  tempObjects.remove(key);
return o;
}

public void addTempObject(String key, Object object) { 
    tempObjects.put(key, object);
}

and cast the Object to Drawable on the way back. You may also add a boolean param in the get(), and remove the object from the map if it is true, that way you can access a certain temp object more than once, or remove it immediately if you are sure that you won't need it anymore in there

EDIT: sorry for the Exception catch, I pasted the code from a function where I have a HashMap<Class<?>, HashMap<String, Object>> for more detailed temp objects getter, where I am getting one hashMap as a value, and then getting the Object from it, that's why there was an NPE check in the code that I pasted first

apps
  • 942
  • 5
  • 8
  • Thanks - I think this is along the right lines. Only slight complication is that I need to access the Application class from my object - but I'm sure I can figure that out. – Paul Hunnisett Nov 17 '10 at 17:46
  • @Paul Hunnisett not a problem: http://stackoverflow.com/questions/4177731/android-how-to-call-method-from-another-class-without-passing-context/4177776#4177776 – apps Nov 17 '10 at 17:47
  • The more I think about this, the more I'm slightly concerned about filling the memory with Drawables. I wondered about overiding finalize in my object and removing the associated Drawable then - but wasn't sure if Parceling actualyl just creates a new Object, in which case my original object would be GC and the Drawable removed. Any ideas on that one? – Paul Hunnisett Nov 18 '10 at 09:33
  • @Paul Hunnisett - when you add a Drawable to the tempObjects, you are only adding a reference to it. At that point, even if you set your original Drawable with "originalDrawable = null" the GC won't clean it because there is still a reference to the real object, in the HashMap. But if you remove it from the tempObjects as well, it will be GC-ed. So: if you use this method to pass objects, and you always make sure to clear the HashMap when you don't need a particular object anymore, is perfectly safe. – apps Nov 18 '10 at 18:28
  • @Paul Hunnisett note that if you pass an Object between 2 Activities that way, both are using the same object, so if you change it in the second, in the first it will be changed too. Often you will get in situations where you don't want this, in them you can clone() the original object (read about cloning in the web if you are not familiar with it) and pass it to the second activity, where it can "edit it". Then, get the cloned item back to the first activity, and replace the original object with the cloned if the user decides to "save the changes" (this is one example for usefullnes of clone) – apps Nov 18 '10 at 18:33
0

You can't pass a complex object that isn't Serializable or Parcelable between activities. One option would be to cache the images in your custom Application class, and access them from there in your activity.

MyApplication application = (MyApplication)getAppliction();
Drawable drawable = application.getCachedDrawable();
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82