On Android 2.1+, I needed to implement a preference that would let the user select a contact from their contacts list. There's not a built-in way to do this. However, I did find what seemed to be a cool way to do this by extending RingtonePreference. A few simple mods, and it was working fine in my phone and on the emulator for various versions of Android.
I was testing on another device, an R2D2, and suddenly I got a force-close when I tried to select a contact. huh? The error was because there was this unknown class "com.motorola.blur.provider.contacts.IdentityModel" that was in the data coming back. Seems that motorola sticks that "foreign" class in the data coming back, and when the RingtonePreference calls "data.getParcelableExtra(...)", I guess the framework is looking through all elements of the set to find the one the RingtonePreference asked for, and it hits the unknown class.
Why even let the RingtonePreference superclass look at things? You have to let it do that because it knows the proper result code to look for. So we must change that.
The solution I'm using for now requires completely overriding and not calling the onActivityResult of RingtonePreference, and hence not doing any extraneous reading of the data that is passed back. Sounds simple, but to do it I needed my custom preference to be able to call startActivityForResult with its own resultCode, which required the custom preference to be able to have a handle to its Activity. RingtonePreference has it made because it has access to its activity via the protected method getPreferenceManager.getActivity().
I'm passing both the Activity and a unique result code to the custom Preference on the PreferenceActivity's onCreate method. Then, the custom preference can start the activity for a result, get the callback for its result code when the called intent is done, and then read just the bit it needs and avoid the weird class. This works, even for the R2D2 craziness. No more BadParcelableException.
Of course, who knows what other manfacturer mods are out there to bite in different ways :)
No comments:
Post a Comment