The android.os.Message
uses a Bundle
to send with it's sendMessage-method. Therefore, is it possible to put a HashMap
inside a Bundle
?

- 37,241
- 25
- 195
- 267

- 2,403
- 4
- 27
- 42
-
java AND android newbie 8) thnx to all! – Marcus Toepper Jul 12 '12 at 13:35
-
Accept any answer if we solved your problem. thx – AMerle Jul 12 '12 at 13:37
-
3Solution is here: [Android - How to pass HashMap
between activities?](http://stackoverflow.com/a/11155142/379693) – Paresh Mayani Jul 12 '12 at 13:37 -
possible duplicate of [Android - How to pass HashMap
between activities?](http://stackoverflow.com/questions/4992097/android-how-to-pass-hashmapstring-string-between-activities) – Andre Bossard Nov 21 '12 at 10:19 -
What is there is a HashMap
>? – RamPrasadBismil Jul 02 '21 at 22:03
7 Answers
try as:
Bundle extras = new Bundle();
extras.putSerializable("HashMap",hashMap);
intent.putExtras(extras);
and in second Activity
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
hashMap = bundle.getSerializable("HashMap");
}
because Hashmap by default implements Serializable
so you can pass it using putSerializable
in Bundle and get in other activity using getSerializable

- 7,360
- 5
- 29
- 41

- 132,198
- 53
- 198
- 213
-
I am getting error when try to serialize the linked hash map Parcel: unable to marshal value com.SGID.DataClass.InvalidSubscriptionReasonsData@41bd2b98 – SweetWisher ツ Oct 04 '13 at 11:48
-
1Can i use this method for object which contains primitive data type and array list of another object?? – SweetWisher ツ Oct 05 '13 at 04:18
-
3
-
2you might want to cast the returned serializable object to hashmap by following ways (HashMap
) – cammando Mar 10 '17 at 13:32 -
4
Please note: If you are using a AppCompatActivity, you will have to call the
protected void onSaveInstanceState(Bundle outState) {}
(NOT public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {}
) method.
Example code...
Store the map:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("leftMaxima", leftMaxima);
outState.putSerializable("rightMaxima", rightMaxima);
}
And receive it in onCreate:
if (savedInstanceState != null) {
leftMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("leftMaxima");
rightMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("rightMaxima");
}
Sorry if it's some kind of a duplicate answer - maybe someone will find it useful. :)

- 12,471
- 9
- 59
- 68
If you want to send all the keys in the bundle, you can try
for(String key: map.keySet()){
bundle.putStringExtra(key, map.get(key));
}

- 5,485
- 1
- 27
- 30
-
I'm curious if there's any reason not to do this? I've not used serialization much, and never bundles (new to android), so I'm curious if this is O(n) and appending to a bundle is effectively O(1) or not. If they're not different in cost, not seeing why you'd bother with serializing it into another map. – Captain Prinny Feb 15 '16 at 23:07
-
@CaptainPrinny unless the map is the only thing you are mapping to the bundle, you might clobber values and you won't know if a key was part of the original map or just a value stored in the bundle. – Jason Sep 27 '18 at 23:36
public static Bundle mapToBundle(Map<String, Object> data) throws Exception {
Bundle bundle = new Bundle();
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (entry.getValue() instanceof String)
bundle.putString(entry.getKey(), (String) entry.getValue());
else if (entry.getValue() instanceof Double) {
bundle.putDouble(entry.getKey(), ((Double) entry.getValue()));
} else if (entry.getValue() instanceof Integer) {
bundle.putInt(entry.getKey(), (Integer) entry.getValue());
} else if (entry.getValue() instanceof Float) {
bundle.putFloat(entry.getKey(), ((Float) entry.getValue()));
}
}
return bundle;
}

- 135
- 11
I am using my kotlin implementation of Parcelable to achieve that and so far it works for me. It is useful if you want to avoid the heavy serializable.
Also in order for it to work, I recommend using it with these
Declaration
class ParcelableMap<K,V>(val map: MutableMap<K,V>) : Parcelable {
constructor(parcel: Parcel) : this(parcel.readMap(LinkedHashMap<K,V>()))
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeMap(map)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ParcelableMap<Any?,Any?>> {
@JvmStatic
override fun createFromParcel(parcel: Parcel): ParcelableMap<Any?,Any?> {
return ParcelableMap(parcel)
}
@JvmStatic
override fun newArray(size: Int): Array<ParcelableMap<Any?,Any?>?> {
return arrayOfNulls(size)
}
}
}
Use
write
val map = LinkedHashMap<Int, String>()
val wrap = ParcelableMap<Int,String>(map)
Bundle().putParcelable("your_key", wrap)
read
val bundle = fragment.arguments ?: Bundle()
val wrap = bundle.getParcelable<ParcelableMap<Int,String>>("your_key")
val map = wrap.map
Don't forget that if your map K,V
are not parceled by default they must implement Parcelable

- 1,522
- 1
- 15
- 30

- 2,041
- 4
- 17
- 28
In Kotlin:
hashMap = savedInstanceState?.getSerializable(ARG_HASH_MAP) as? HashMap<Int, ValueClass>
putSerializable(ARG_HASH_MAP, hashMap)

- 26,736
- 15
- 188
- 224