49

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");
......
......
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • http://stackoverflow.com/questions/4154744/hashmap-of-weakreferences-for-passing-data-between-activities 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. – Pankaj Kumar Feb 14 '11 at 12:42
  • @pankaj i am not getting anything from the above link code – Paresh Mayani Feb 14 '11 at 12:46
  • @Tanmay please don't change the OP's code. Add a comment or answer outlining the change instead. (Rejected edit) – Pekka Feb 14 '11 at 12:48
  • @Pekka No problem.I just wanted to make sure the right thing – Tanmay Mandal Feb 14 '11 at 12:51
  • Can you prefer to make your hashMap as public static? I can explain it. – Pankaj Kumar Feb 14 '11 at 12:53
  • @PM- Paresh Mayani: This might be helpful for you: http://stackoverflow.com/questions/2906925/android-how-do-i-pass-an-object-from-one-activity-to-another I dont found any direct method to do this instead of creating `static HashMap` – Vikas Patidar Feb 14 '11 at 12:53
  • @Vikas i already know that how do we pass values, arraylist , object from one activity to another, but i dont know this only. Thanx – Paresh Mayani Feb 14 '11 at 12:56
  • @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 an `ArrayList` 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. – Vikas Patidar Feb 14 '11 at 13:12

5 Answers5

75

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.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
st0le
  • 33,375
  • 8
  • 89
  • 89
  • 1
    Excause me. What if I can have a HaspMap? Can it be serialize? The value Objects are just int, String or float type. No custom Object type. – Yeung May 15 '13 at 09:01
  • Keep 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. – Miha_x64 Feb 10 '17 at 14:21
  • "You will need to Cast the return value" -- that's what I was missing. – arlomedia Sep 28 '17 at 08:35
  • I'm getting "Unchecked cast" warning – temirbek Oct 09 '17 at 07:03
  • The "Unchecked cast" warning is basically saying "you're on your own" ;) – Aba Nov 10 '17 at 07:15
67

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");
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
4

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.

MetaSnarf
  • 5,857
  • 3
  • 25
  • 41
1

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.

MattC
  • 12,285
  • 10
  • 54
  • 78
0

Here I am showing sample code for your reference. I just tried this code, it works fine for me. Check this :

MainActivity :

    final HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
    hashMap.put(1, "Hi");

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub              

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("h", hashMap);
            startActivity(intent);

        }
    });

SecondActivity :

Toast.makeText(SecondActivity.this,"Hi " +  getIntent().getSerializableExtra("h").toString(),Toast.LENGTH_SHORT).show();
BackStabber
  • 227
  • 1
  • 13
VVB
  • 7,363
  • 7
  • 49
  • 83