Artem
Artem

Reputation: 4639

Cannot read Parcelable from Intent in onActivityReenter

I want to return my address model as result in Intent. If I try get my address model in onAcivityResult methods all works fine, but in onActivityReenter I got this Exception:

Class not found when unmarshalling: ua.com.uklontaxi.models.UIAddress
java.lang.ClassNotFoundException: ua.com.myapp.models.UIAddress
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:324)
    at android.os.Parcel.readParcelableCreator(Parcel.java:2383)
    at android.os.Parcel.readParcelable(Parcel.java:2337)
    at android.os.Parcel.readValue(Parcel.java:2243)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:2592)
    at android.os.BaseBundle.unparcel(BaseBundle.java:221)
    at android.os.Bundle.getParcelable(Bundle.java:786)

There are not such errors in another places (for example onActivityResult in the same Activity)

How to fix it?

P.S. I put my model to Bundle and then I put this bundle to Intent. I tried put address to Intent without Bundle-wrapping. It doens't help me.

Upvotes: 1

Views: 144

Answers (2)

Haris H
Haris H

Reputation: 134

For anyone still wondering why this issue persists only for API versions below Oreo (8), it has to do with a fix that was provided for API 8+. You can find the official fix here or you can check the below code (taken from the official repo):

Intent intent = mEnterActivityOptions.getResultData();
   if (intent != null) {
        intent.setExtrasClassLoader(activity.getClassLoader());
    }
activity.onActivityReenter(result, intent);

It essentially, adds manually the ClassLoader to the Intent, before calling the Activity's onActivityReenter(...);

Upvotes: 1

Artem
Artem

Reputation: 4639

This helps me:

override fun onActivityReenter(resultCode: Int, data: Intent?) {
   super.onActivityReenter(resultCode, data)

   data?.setExtrasClassLoader(this.classLoader) // this is context!
}

Upvotes: 2

Related Questions