When reading an object from a parcel in Android, when we not want to use the default ClassLoader?

StackOverflow https://stackoverflow.com/questions/18772841

When implementing the Parcelable interface, the CREATOR implementation typically uses a constructor that takes in a Parcel e.g.

public Foo(Parcel parcel)

In this parcel, we might have code like

mMyField = parcel.readParcelable(MyFoo.class.getClassLoader())

As far as I can tell, it's usually ok to just use the default class loader and pass in null instead since parcel.writeParcelable(mMyField) already stores the class name in the parcel.

So my question is, when would I need to use MyFoo.class.getClassLoader() instead of passing in null?

有帮助吗?

解决方案

Since a Parcel stores info about its class, but not the class itself, that class will be loaded from a name, like java/util/ArrayList. If your class is nt on the classpath or the .class file is on a remote server even you will need to use a different ClassLoader that can access it, for example URLClassLoader.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top