I'm attempting to pass a serialized LinkedHashMap between activities, and getting a confusing result/error when I deserialize the object.
I serialize the object as follows:
Bundle exDetails = new Bundle();
LinkedHashMap<String, Exercise> exMap = new LinkedHashMap<String,
Exercise (workout.getExercises());
exDetails.putString(WORKOUTNAME, workout.getWorkoutName());
exDetails.putSerializable(workout.getWorkoutName(), exMap);
iComplete.putExtra("wName", exDetails);
startActivity(iComplete);
That seems to work fine, the problem shows up in the next activity:
Bundle exDetails = getIntent().getBundleExtra("wName");
workoutName = exDetails.getString(WORKOUTNAME);
Serializable eData = exDetails.getSerializable(workoutName);
ex = new LinkedHashMap<String, Exercise>();
ex = (LinkedHashMap<String, Exercise>) eData;
At this point, the deserialized object (eData) contains a HashMap object (not LinkedHashMap), and it gives me
java.lang.ClassCastException:java.util.HashMap cannot be
cast to java.util.LinkedHashMap
on that last line. I've verified with the debugger that the bundle (in the second activity) contains a HashMap, instead of a LinkedHashMap (as I'm assuming it should). I should also mention that I need to maintain the order in which entries are added to the Map, hence the usage of LinkedHashMap. The entries eventually get printed, and order is very important for the output.
Questions: Am I doing anything wrong in particular, or is this problem due to bugs with LinkedHashMap's serialization? I've noticed a few similar threads, that seem to speak of this being an ongoing problem with several of the Map implementations. They didn't answer my problem directly though.
If the latter, is there a workaround that isn't too advanced (I'm not far beyond beginner level, but I'm willing to try most things), or do I need to just bite the bullet and work something other than LinkedHashMap?
P.s. I tried to include everything relevant, but I can add more code if I left out anything important.
LInkedHashMap
to aHashmap
when it is serialized/deserialized through theBundle
. See my answer the this question and the linked question for more gory details: https://mcmap.net/q/752431/-sending-linkedhashmap-to-intent – Bobo