I have the following class:
public class IdList extends ArrayList<Long> {
public IdList() {
super();
}
public IdList(Collection<Long> idCollection) {
super(idCollection);
}
}
I pass an instance of this class to an IntentService with the following code:
final Intent intent = new Intent(this, MyService.class);
intent.putExtra(MyService.IDS, ids);
startService(intent);
And i use the following code to fetch it in the Service:
IdList ids = (IdList) intent.getSerializableExtra(IDS);
Which results in the following ClassCastException:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to my.package.IdList
BUT
Sending an object of the same type (IdList) from the Service by using LocalBroadcastManager and fetching it in onReceive() (via the same method, by calling getSerializableExtra() on the Intent and casting to IdList) works flawlessly.
No ClassCastException thrown.
Why is this happening? Is this a bug in Android?
EDIT:
I'm using the following method to instantiate my IdList object:
public IdList getIds() {
return selectedIds.isEmpty() ? null : new IdList(selectedIds);
}
selectedIds is a HashSet<Long> object.
EDIT2:
I'm not looking for a workaround. I'd like to know why the same code works perfectly fine when the Intent is sent through a broadcast and why does it throw a ClassCastException when it is passed through the startService() call.