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")); }
JesusFreke Sep 28 '11 at 4:29 2011-09-28 04:29
source share