sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

Fixed ClassNotFoundException during MosbySavedState restoration by properly passing the ClassLoader #217

Closed cbeyls closed 7 years ago

cbeyls commented 7 years ago

This fixes the crash described here.

This is not a bug in the framework or the support library. A ClassLoader must always be passed to any Parcelable that uses Parcel.readParcelable() if the class to read is not a framework class. This includes the superState passed to AbsSavedState or BaseSavedState through super().

1) Using ParcelableCompat.newCreator() we are able to get a ClassLoader from the system. This is a thin wrapper around the native Parcelable.ClassLoaderCreator. We still check if it's null and provide a default one to be on the safe side, but it should never be null in API 13+ if the Parcelable is created from a properly initialized Bundle. 2) Using android.support.v4.view.AbsSavedState instead of View.BaseSavedState we are able to pass this ClassLoader to super() so it's used to create the superState.

sockeqwe commented 7 years ago

Thank you so much and for the detailed explanation! This explains a lot. I was under the illusion that the ClassLoader from the Bundle (which is set activity or fragment during restoration) will be used somehow "magically" internally as the view's state is restored from that bundle.

Again, thank you very much!