Open ghost opened 6 years ago
The problem is that when Android serializes the Bundle when saving or restoring the state, it only supports a subset of types: Integer, Long, String, Map, Bundle, List.
If you serialize an ArrayList of LinkedHashMap it will be restored as List and Map. This is problematic if using LinkedHashMap because you loose the element ordering.
See: https://stackoverflow.com/questions/12300886/linkedlist-put-into-intent-extra-gets-recast-to-arraylist-when-retrieving-in-nex/12305459#12305459 for more details
You can use a custom bundler to avoid the problem:
Create a custom bundler:
public class LinkedHashmapBundler implements Bundler<LinkedHashMap> {
@Override
public void put(String s, LinkedHashMap val, Bundle bundle) {
bundle.putSerializable(s, new Wrapper<>(val));
}
@SuppressWarnings("unchecked")
@Override
public LinkedHashMap get(String s, Bundle bundle) {
return ((Wrapper<LinkedHashMap>) bundle.getSerializable(s)).get();
}
}
public class Wrapper<T extends Serializable> implements Serializable {
private T wrapped;
Wrapper(T wrapped) {
this.wrapped = wrapped;
}
public T get() {
return wrapped;
}
}
Use it like this:
@State(LinkedHashmapBundler.class) LinkedHasMap map
Thank you, it works. I've noticed something strange. If you have project that uses android library and that library defines icepick processor and annotationProcessor, then icepick won't work inside the app's domain. Once annotationProcessor is duplicated to app module, everything works.
I get an error "java.lang.ClassCastException" when restoring activity.
@State LinkedHashMap<Double, String[]> dropsMap;
How can this be avoided?