I'm looking for a way to convert a Bundle to a HashMap. I'm surprised that such a method is not in the docs.
Is not the Bundle equivalent of a Map, where you have String keys and Object values? Can it be theoretically converted to a mapping? What is the way to do this?
edit: Is the relation between key and value in Bundle unambiguous?
Is not the Bundle equivalent of a Map, where you have String keys and Object values?
First, a Map
does not necessarily have String
keys.
Second, while a Map
can hold arbitrary objects, it cannot do so in a typesafe fashion. IOW, if you want the Map
to hold strings, integers, and so forth, you will need to keep casting the results of get()
calls, because your Map
would have to be Map<String, Object>
.
Third, Bundle
implements Parcelable
, which is very important for Android IPC. Map
is an interface and does not extend Parcelable
.
Can it be theoretically converted to a mapping?
Um, sure, though I fail to see what the benefit would be.
What is the way to do this?
Iterate over the keySet()
and call get()
on the Bundle
for each key, putting the result into a Map<String, Object>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With