0

I have a problem with try to restore instance in my singleton object in Android. I have primitive variable and list of objects.

This is my object:

 String idUsuario, email, idDevice, nombreUsuario, currency;
        List<ObjectExample> list;
        long fechaCreacion;
        double valorUnidad;
        int premium;

My parcel implementation is:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(idUsuario);
    dest.writeString(email);
    dest.writeString(idDevice);
    dest.writeString(nombreUsuario);
    dest.writeString(currency);
    dest.writeLong(fechaCreacion);
    dest.writeDouble(valorUnidad);
    dest.writeInt(premium);
    dest.writeList(list);
}


protected User_Singleton(Parcel in) {
    idUsuario = in.readString();
    email = in.readString();
    idDevice = in.readString();
    nombreUsuario = in.readString();
    currency = in.readString();
    fechaCreacion = in.readLong();
    valorUnidad = in.readDouble();
    premium = in.readInt();
    list = new ArrayList<ObjectExample>();
    in.readList(list, ObjectExample.class.getClassLoader());
}

public static final Creator<User_Singleton> CREATOR = new Creator<User_Singleton>() {
    @Override
    public User_Singleton createFromParcel(Parcel in) {
        return new User_Singleton();
    }

    @Override
    public User_Singleton[] newArray(int size) {
        return new User_Singleton[size];
    }
};

And my ObjectExample:

int monthNumber, yearNumber;
double bank;

public ObjectExample() {
}

public ObjectExample(int monthNumber, int yearNumber, double bank) {
    this.monthNumber = monthNumber;
    this.yearNumber = yearNumber;
    this.bank = bank;
}

protected ObjectExample(Parcel in) {
    monthNumber = in.readInt();
    yearNumber = in.readInt();
    bank = in.readDouble();
}

public static final Creator<ObjectExample> CREATOR = new Creator<ObjectExample>() {
    @Override
    public ObjectExample createFromParcel(Parcel in) {
        return new ObjectExample(in);
    }

    @Override
    public ObjectExample[] newArray(int size) {
        return new ObjectExample[size];
    }
};

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(monthNumber);
    dest.writeInt(yearNumber);
    dest.writeDouble(bank);
}

And I got this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{example.Principal.MainActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@e7eb28e: Unmarshalling unknown type code 6619252 at offset 4036
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2836)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
 at android.app.ActivityThread.-wrap11(Unknown Source:0)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
 at android.os.Handler.dispatchMessage(Handler.java:105)
 at android.os.Looper.loop(Looper.java:164)
 at android.app.ActivityThread.main(ActivityThread.java:6665)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@e7eb28e: Unmarshalling unknown type code 6619252 at offset 4036
 at android.os.Parcel.readValue(Parcel.java:2754)
 at android.os.Parcel.readArrayMapInternal(Parcel.java:3042)
 at android.os.BaseBundle.unparcel(BaseBundle.java:257)
 at android.os.BaseBundle.getBoolean(BaseBundle.java:834)
 at android.app.Activity.restoreHasCurrentPermissionRequest(Activity.java:7305)
 at android.app.Activity.performCreate(Activity.java:7056)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2789)
 ... 9 more

When phone restart, that error appears. I think is the list, but which is the problem?

Thank you so much.

Traif
  • 671
  • 2
  • 6
  • 12

1 Answers1

1

In your User_Singletone pass parameter (Parcel in) in constrictor

public static final Creator<User_Singleton> CREATOR = new Creator<User_Singleton>() {
    @Override
    public User_Singleton createFromParcel(Parcel in) {
        //return new User_Singleton();
        return new User_Singleton(in);
    }

    @Override
    public User_Singleton[] newArray(int size) {
        return new User_Singleton[size];
    }
};
SergeyBukarev
  • 1,478
  • 1
  • 12
  • 19
  • Hi, I'll try. Can I force the restore on my phone? – Traif Jul 16 '18 at 18:21
  • For testing you can create new Bundle() and call putParcelable in their. Or configure you device in developer settings ( something like - always destroy activity) – SergeyBukarev Jul 16 '18 at 18:38