How to send hashmap value to another activity using an intent

71,691

Solution 1

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"));
}

Solution 2

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.

Share:
71,691

Related videos on Youtube

Piyush
Author by

Piyush

i like android

Updated on April 13, 2020

Comments

  • Piyush
    Piyush about 4 years

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

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

    • naresh
      naresh almost 13 years
      Hi, you are sending which value(int, string,double..)?
    • Piyush
      Piyush almost 13 years
      means string value i want send
    • ngesh
      ngesh almost 13 years
      @Piyush.. in Addition JesusFreke's answer do this to get values, String[] val = new String[hashMap.size]; (hasMap.values).toArray(val);
    • ilango j
      ilango j almost 13 years
      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.
  • R Earle Harris
    R Earle Harris almost 11 years
    Note that HashMaps serialize. Maps, apparently, don't.
  • JesusFreke
    JesusFreke almost 11 years
    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.
  • marienke
    marienke over 10 years
    Hi, I'm sending a HashMap<String, Object> 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<String, Object>)intent.getSerializableExtra("map"); returns null. Is it because I'm using HashMap<String, Object> or because I'm sending it from an Activity that was created for result from another Activity?
  • inexcii
    inexcii almost 10 years
    @marienke I used HashMap<String, Object> in this way in my project and it works fine. I guess your issue is probably the latter one, good luck.
  • Skynet
    Skynet almost 9 years
    I get a cast warning this way
  • ban-geoengineering
    ban-geoengineering almost 8 years
  • Anish Kumar
    Anish Kumar about 7 years
    Hey hi, I am getting parcel error at runtime. java.lang.RuntimeException: Parcel: unable to marshal value I am passing HaspMap<String, JSONArray>
  • JesusFreke
    JesusFreke about 7 years
    @AnishKumar JSONArray is not serializable (it does not implement the Serializable interface)