Android - How to pass HashMap<String,String> between activities?
Solution 1
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.
Solution 2
Solution:
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");
Solution 3
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.
Solution 4
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.
Related videos on Youtube
Paresh Mayani
Mobile Development & Project Management/Delivery Consultant | Co-Founder | Start-up Consultant | Community Organiser @ GDG Ahmedabad | Certified Scrum Master Exploring the horizon of the software industry since 11+ years. Let's connect over twitter: @pareshmayani OR LinkedIn What I do: Co-Founder / CEO @ SolGuruz Founder/Organiser, Google Developers Group, Ahmedabad Blog @ TechnoTalkative Achievements: 11th person to earn Android gold badge 4th person to earn android-layout bronze badge. 20th person in the list of Highest reputation holder from India
Updated on July 06, 2020Comments
-
Paresh Mayani almost 4 years
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"); ...... ......
-
Pankaj Kumar over 13 yearsstackoverflow.com/questions/4154744/… this will help you. And in another way make your HashMap as public and static, set its values in caller activity and use its values into called activity. And before adding values to your HashMap, clear its previous values.
-
Paresh Mayani over 13 years@pankaj i am not getting anything from the above link code
-
Pekka over 13 years@Tanmay please don't change the OP's code. Add a comment or answer outlining the change instead. (Rejected edit)
-
Tanmay Mandal over 13 years@Pekka No problem.I just wanted to make sure the right thing
-
Pankaj Kumar over 13 yearsCan you prefer to make your hashMap as public static? I can explain it.
-
Vikas Patidar over 13 years@PM- Paresh Mayani: This might be helpful for you: stackoverflow.com/questions/2906925/… I dont found any direct method to do this instead of creating
static HashMap
-
Paresh Mayani over 13 years@Vikas i already know that how do we pass values, arraylist , object from one activity to another, but i dont know this only. Thanx
-
Vikas Patidar over 13 years@PM Paresh Mayani: As per your question, I really don't think you need to pass the
HashMap
instead simply your can do this by putting anArrayList<String>
in intent and on the other activity you can obtain the value form those positions , assuming that you know which data is stored at the perticular position.
-
-
Paresh Mayani over 13 yearsthanx for the support, and "new" information , but i just need to pass HashMap between two activities i.e. From activity A to activity B
-
Nitish Hardeniya over 13 yearsI figured. Just wanted to offer it as an alternative.
-
Yeung about 11 yearsExcause me. What if I can have a HaspMap<String, Object>? Can it be serialize? The value Objects are just int, String or float type. No custom Object type.
-
Paresh Mayani about 10 yearsAre you sure that you are able to get HashMap<String, String> using getExtras.get("key") ?
-
MetaSnarf about 10 yearsyeah...as long as you passed "HashMap<String,String>" to the receiving activity
-
Miha_x64 over 7 yearsKeep in mind, that the map will not be serialized. It will be parcelized by internals if possible. And then you will get a HashMap on the other side, despite you've put LinkedHashMap or any other map.
-
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> -
arlomedia almost 7 years"You will need to Cast the return value" -- that's what I was missing.
-
temirbek over 6 yearsI'm getting "Unchecked cast" warning
-
Aba over 6 yearsThe "Unchecked cast" warning is basically saying "you're on your own" ;)
-
Abubakar almost 6 years@anish bcz JSONArray is not serializable.
-
Anish Kumar almost 6 yearsThanks mate, though it is a late reply, appreciated your help. :) @Abubakar