Paul Hunnisett
Paul Hunnisett

Reputation: 908

Need to pass an un parcelable object between activities

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?

Upvotes: 0

Views: 2721

Answers (3)

oznus
oznus

Reputation: 2486

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

Upvotes: 1

apps
apps

Reputation: 942

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

Upvotes: 1

Cheryl Simon
Cheryl Simon

Reputation: 46844

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();

Upvotes: 0

Related Questions