Weird ClassCastException on deserialization on onRestoreInstanceState

In onSaveInstanceState():

// departures is instance of Departures which extends ArrayList
bundle.putSerializable("departures", departures);

In onRestoreInstanceState:

departures = (Departures) state.getSerializable("departures");

When I rotate the screen, the activity restarts and the state is restored. It is working fine. If I leave the event, after a while Android will delete it from memory and save its state. When I get back to him, it will work in onRestoreInstanceState:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to cz.fhejl.pubtran.Departures

getSerializablenow returns ArrayList, not Departures.

Edit: here is what Departures.java looks like: http://pastebin.com/qc3QfrK7

+5
source share
2 answers

, , , Android . Parcel#writeValue(Object), if/else, if (o instanceof List) - instanceof Serializable. Departures List, , , .

-, List, .

+6

, , .

, .

private class ArrayHolder implements Serializable{
  public Serializable[] _myArray;
  public ArrayHolder(Serializable myArray){
    _myArray = myArray;
  }
}

, , ,

bundle.putSerializable("name",new ArrayHolder(arrayToBeSaved));

:

ArrayHolder arrayHolder = (ArrayHolder) bundle.getSerializable("name");
arrayRecovered = arrayHolder._myArray;

, , .

+4

All Articles