ClassCastException occurs randomly to restore Vector in onRestoreInstanceState(). Generally restoring vector is finished well, but sometimes exception occurs.
I think it happens when activity is went to background and destroyed but I'm not sure.
Any ideas? Thank you.
Stack<LocationInfo> mLocationInfoVector;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable("locationInfos", mLocationInfoVector);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getSerializable("locationInfos") != null) {
@SuppressWarnings("unchecked")
mLocationInfoVector= (Stack<LocationInfo>) savedInstanceState.getSerializable("locationInfos");
}
super.onRestoreInstanceState(savedInstanceState);
}
ADDED:
I forgot to attach exception log. That is
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Stack
onRestoreInstanceState
method as follows:Object saved = savedInstanceState.getSerializable("locationInfos"); if (saved instanceof Stack) { mLocationInfoVector = (Stack<LocationInfo>) saved; } else if (saved != null) { Log.e("RestoreBug", "Restored object class: " + saved.getClass().getName()); }
Then you can at least see what you're getting back. (Sorry for posting code in a comment; I don't want to post this as an answer). – BertoldeonSaveInstanceState
? I'm wondering if you're using the"locationInfos"
key anywhere else in your code. (P.S. in any event, you should reorganize youronRestoreInstanceState
code so that it only callsgetSerializable()
once.) – Bertolde