How to send hashmap value to another activity using intention

How to send a HashMap value from one intention to the second intention?

Also, how to get this HashMap value in the second step?

+43
android hashmap
Sep 28 '11 at 4:15
source share
2 answers

The Java HashMap class extends the Serializable interface, making 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 call Intent.getSerializableExtra(String) with the name that you used with putExtra.

For example, when sending an 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 host operation:

 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")); } 
+137
Sep 28 '11 at 4:29
source share

I hope this works too.

in sending operation

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

in host activity

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

when I submit a hashmap as below

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

Hope this helps someone.

0
Dec 01 '17 at 18:18
source share



All Articles