问题
I'm trying to pass a Hashmap of custom objects through an intent.
I am trying to use parcelable as that's what i read is the correct way to do things in android (i.e. parcelable over serializable).
However, when i try and get the hashmap using getParcelable, it can't find my hashmap in the intent, but getSerializable can.
Is it OK to use serializable for hashmaps? Is this my only option?
回答1:
You can of course serialize your map, as long as your Objects are serializable. What you also could do is creating a separate singleton class (DataStore or so?) that saved your data. This way you don't need to pass the data from Activity to Activity. Or, even simpler, save the data in a public static variable.
回答2:
java.util.HashMap is not Parcelable, so your options are:
- use put/getSerializable, which will work fine within your app
- encode the HashMap as json or another remotable format if sending outside your app
- take out the map entries one by one and put them in the Bundle directly (assuming they have string keys)
来源:https://stackoverflow.com/questions/19737343/android-passing-hashmap-through-intent