Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't android's Bundle functionally equivalent with a Map?

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?

like image 665
n611x007 Avatar asked Nov 29 '13 14:11

n611x007


1 Answers

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>.

like image 148
CommonsWare Avatar answered Nov 20 '22 22:11

CommonsWare