How to send hashmap value to another activity using an intent
Asked Answered
I

2

90

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

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

Impure answered 28/9, 2011 at 4:15 Comment(4)
Hi, you are sending which value(int, string,double..)?Dotti
means string value i want sendImpure
@Piyush.. in Addition JesusFreke's answer do this to get values, String[] val = new String[hashMap.size]; (hasMap.values).toArray(val);Ribald
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.Pyrometer
F
223

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"));
}
Flitting answered 28/9, 2011 at 4:29 Comment(8)
Note that HashMaps serialize. Maps, apparently, don't.Bedlam
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.Flitting
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?Superfuse
@Superfuse 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.Edette
I get a cast warning this wayIncentive
@Skynet, see #509576 . :-)Malpractice
Hey hi, I am getting parcel error at runtime. java.lang.RuntimeException: Parcel: unable to marshal value I am passing HaspMap<String, JSONArray>Radmilla
@AnishKumar JSONArray is not serializable (it does not implement the Serializable interface)Flitting
E
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.

Evangelin answered 1/12, 2017 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.