I have a LinkedHashMap that I want to pass through the Bundle savedInstanceBundle to store it between screen rotations. How do I do this safely? Before I just cast it because I know what I'm putting into it and what I'm getting out of it, but this did generate a warning that it was considered an unsafe cast.
What I am doing now :
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
if(thumbnails != null) {
savedInstanceState.putSerializable("thumbnails", thumbnails);
}
}
and retrieving it
thumbnails = (LinkedHashMap<Long, Bitmap>)savedInstanceState.getSerializable("thumbnails");
What is the correct way to deserialize LinkedHashMaps from a Bundle? Can it be done at all? If not, how do I preserve order without adding another layer in between that keeps track of the position which would involve changing a lot of existing code?
` Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.LinkedHashMap`
From the MVC (Model -- View -- Controller) point of view, an Activity
is a Controller. (And so is a Fragment
.) The View is made from the XML and the Android View
subclasses that you reuse (people rarely define custom View
subclasses). And the Model is --- well, you have to define a class for the Model yourself! If you had Model, you'd look from a different perspective.
But if you nevertheless want to pass data from one Activity incarnation to another -- well, why don't you use a JSONObject
/JSONArray
? It's an overkill, it will be slow, but it should at least work.
Another possibility is to convert the LinkedHashMap
into a list of key-value pairs and later reconstruct it from that list.