• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Extending the Intent ... received ClassCastException

I am attempting to create an enhanced Intent class (I call it DataIntent) by giving it the ability to hold a "payload" of type Object (versus using it's built-in facility for Uri's). My DataIntent extends Android's Intent class.

My Activity creates the extended object without any problems and the invocation of the startActivityForResult() goes off without any problems. But, in my responding Activity when I call the getIntent() method, and attempt to cast it to my DataIntent, I'll throw the ClassCastException.

I realize this is probably a very dumb question - a 1,000 appologies in advance - but does anyone know why I cannot cast it to the DataIntent since that's what was used to start the new Activity, and DataIntent is a child of Intent?

DataIntent dataIntent = (DataIntent)getIntent(); // invoked inside a Activity instance - throws ClassCastException
 
Well this was a bit disappointing ... I was hoping someone here would know the answer. Nope.

I did manage to get the answer, though, by going to the StackOverflow Android forum. A big thank you to "hackbod" for providing the proper reason of the exception.

Turns out that since the Intent object is designed to move across process boundries whatever reference you start wtih your receiving Activity will have a different one.

This makes sense when you think about your application being able to use a Contact list picker, or to bring up the phone dialer, or etc., etc., etc. Examples of your application sending an Intent to another process, which would invalidate any object references from being the same. Thus, the ClassCastException.

FYI .......
 
Back
Top Bottom