Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending LinkedHashMap to intent

I want send LinkedHashMap to another Intent. But I don't known what method for extras is allowable.

Bundle extras = getIntent().getExtras();
  LinkedHashMap<Integer, String[]> listItems = extras.get(LIST_TXT);
like image 897
frstua Avatar asked Nov 12 '10 11:11

frstua


People also ask

How LinkedHashMap works internally in Java?

How LinkedHashMap Work Internally? Hash: All the input keys are converted into a hash which is a shorter form of the key so that the search and insertion are faster. Key: Since this class extends HashMap, the data is stored in the form of a key-value pair. Therefore, this parameter is the key to the data.

Is LinkedHashMap synchronized?

Just like HashMap, LinkedHashMap implementation is not synchronized. So if you are going to access it from multiple threads and at least one of these threads is likely to change it structurally, then it must be externally synchronized.

How is LinkedHashMap implemented in Java?

Answer: LinkedHashMap in Java is implemented as a combination of the HashTable and LinkedList. It implements the map interface. It has a predictable iteration order. It internally uses a doubly-linked list for entries.


1 Answers

You cannot reliably send a LinkedHashMap as an Intent extra. When you call putExtra() with a LinkedHashMap, Android sees that the object implements the Map interface, so it serializes the name/value pairs into the extras Bundle in the Intent. When you want to extract it on the other side, what you get is a HashMap, not a LinkedHashMap. Unfortunately, this HashMap that you get has lost the ordering that was the reason you wanted to use a LinkedHashMap in the first place.

The only reliable way to do this is to convert the LinkedHashMap to an ordered array, put the array into the Intent, extract the array from the Intent on the receiving end, and then recreate the LinkedHashMap.

See my answer to this question for more gory details.

like image 102
David Wasser Avatar answered Oct 27 '22 19:10

David Wasser