Android - passing hashmap through intent

谁说胖子不能爱 提交于 2020-01-25 11:43:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!