89

How to send HashMap value from one Intent to second Intent?

Also, how to retrieve that HashMap value in the second Activity?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Piyush
  • 3,061
  • 8
  • 34
  • 52
  • 1
    Hi, you are sending which value(int, string,double..)? – naresh Sep 28 '11 at 04:26
  • means string value i want send – Piyush Sep 28 '11 at 04:33
  • @Piyush.. in Addition JesusFreke's answer do this to get values, String[] val = new String[hashMap.size]; (hasMap.values).toArray(val); – ngesh Sep 28 '11 at 04:36
  • we can't send hash map directly via intent. For alternative create two array list one is to hold keys and other is to hold values. Now send these two array list via intent, in the other class you will get two array lists, now create a empty Hashmap and add key,value. To get key and value loop your keys arraylist for corresponding key get value from values arraylist. – ilango j Sep 28 '11 at 04:38

2 Answers2

222

Java's HashMap class extends the Serializable interface, which makes it easy to add it to an intent, using the Intent.putExtra(String, Serializable) method.

In the activity/service/broadcast receiver that receives the intent, you then call Intent.getSerializableExtra(String) with the name that you used with putExtra.

For example, when sending the intent:

HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

And then in the receiving Activity:

protected void onCreate(Bundle bundle) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
    Log.v("HashMapTest", hashMap.get("key"));
}
JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • 28
    Note that HashMaps serialize. Maps, apparently, don't. – R Earle Harris Aug 22 '13 at 15:37
  • 6
    Map is an interface - you can't serialize an interface, only a specific implementation of it. In this case, Map doesn't implement/extend the Serializable interface itself, so it's up to the specific implementation whether it wants to implement Serializable or not. And HashMap does implement it. – JesusFreke Aug 22 '13 at 18:54
  • 1
    Hi, I'm sending a HashMap as a serializable extra from an Activity I started for result from another activity. So I'm returning an intent on result. When I try to retrieve the HashMap from the intent, (HashMap)intent.getSerializableExtra("map"); returns null. Is it because I'm using HashMap or because I'm sending it from an Activity that was created for result from another Activity? – marienke Nov 07 '13 at 16:21
  • 1
    @marienke I used HashMap in this way in my project and it works fine. I guess your issue is probably the latter one, good luck. – inexcii Aug 13 '14 at 07:14
  • 3
    I get a cast warning this way – Skynet Aug 13 '15 at 08:38
  • @Skynet, see http://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings . :-) – ban-geoengineering Sep 10 '16 at 13:43
  • 1
    Hey hi, I am getting parcel error at runtime. java.lang.RuntimeException: Parcel: unable to marshal value I am passing HaspMap – Anish Kumar Apr 24 '17 at 11:09
  • 1
    @AnishKumar JSONArray is not serializable (it does not implement the Serializable interface) – JesusFreke Apr 26 '17 at 17:45
7

I hope this must work too.

in the sending activity

Intent intent = new Intent(Banks.this, Cards.class);
intent.putExtra("selectedBanksAndAllCards", (Serializable) selectedBanksAndAllCards);
startActivityForResult(intent, 50000);

in the receiving activity

Intent intent = getIntent();
HashMap<String, ArrayList<String>> hashMap = (HashMap<String, ArrayList<String>>) intent.getSerializableExtra("selectedBanksAndAllCards");

when I am sending a HashMap like following,

Map<String, ArrayList<String>> selectedBanksAndAllCards = new HashMap<>();

Hope it would help for someone.

alexrnov
  • 2,346
  • 3
  • 18
  • 34