How to pass the detail
HashMap to another Activity?
HashMap<String,String> detail = new HashMap<String, String>();
detail.add("name","paresh");
detail.add("surname","mayani");
detail.add("phone","99999");
......
......
This is pretty simple, All Collections
objects implement Serializable
(sp?) interface
which means they can be passed as Extras inside Intent
Use putExtra(String key, Serializable obj)
to insert the HashMap
and on the other Activity
use getIntent().getSerializableExtra(String key)
, You will need to Cast the return value as a HashMap
though.
Sender Activity:
HashMap<String, String> hashMap= adapter.getItem(position);
Intent intent = new Intent(SourceActivity.this, DestinationActivity.class);
intent.putExtra("hashMap", hashMap);
startActivity(intent);
Receiver Activity:
Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) intent.getSerializableExtra("hashMap");
i used this to pass my HashMap
startActivity(new Intent(currentClass.this,toOpenClass.class).putExtra("hashMapKey", HashMapVariable));
and on the receiving activity write
HashMap<String,String> hm = (HashMap<String,String>) getIntent().getExtras().get("hashMapKey");
cuz i know my hashmap contains string as value.
An alternative is if the information is something that might be considered "global" to the application, to then use the Application class. You simply extend it and then define your custom class in your manifest using the <application> tag. Use this sparingly, though. The urge to abuse it is high.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With