开发者

Need to pass an un parcelable object between activities

开发者 https://www.devze.com 2023-01-26 02:39 出处:网络
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.

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开发者_StackOverflow中文版 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?


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号