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.
Related videos on Youtube
Comments
-
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 almost 13 yearsHi, you are sending which value(int, string,double..)?
-
Piyush almost 13 yearsmeans string value i want send
-
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 almost 13 yearswe 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 almost 11 yearsNote that HashMaps serialize. Maps, apparently, don't.
-
JesusFreke almost 11 yearsMap 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 over 10 yearsHi, 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 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 almost 9 yearsI get a cast warning this way
-
ban-geoengineering almost 8 years@Skynet, see stackoverflow.com/questions/509076/… . :-)
-
Anish Kumar about 7 yearsHey hi, I am getting parcel error at runtime. java.lang.RuntimeException: Parcel: unable to marshal value I am passing HaspMap<String, JSONArray>
-
JesusFreke about 7 years@AnishKumar JSONArray is not serializable (it does not implement the Serializable interface)