51

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");
......
......
8
  • stackoverflow.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. Commented Feb 14, 2011 at 12:42
  • @pankaj i am not getting anything from the above link code Commented Feb 14, 2011 at 12:46
  • @Tanmay please don't change the OP's code. Add a comment or answer outlining the change instead. (Rejected edit) Commented Feb 14, 2011 at 12:48
  • @Pekka No problem.I just wanted to make sure the right thing Commented Feb 14, 2011 at 12:51
  • Can you prefer to make your hashMap as public static? I can explain it. Commented Feb 14, 2011 at 12:53

5 Answers 5

77

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Excause 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.
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.
"You will need to Cast the return value" -- that's what I was missing.
I'm getting "Unchecked cast" warning
The "Unchecked cast" warning is basically saying "you're on your own" ;)
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");

3 Comments

Hey hi, I am getting parcel error at runtime. java.lang.RuntimeException: Parcel: unable to marshal value I am passing HaspMap<String, JSONArray>
@anish bcz JSONArray is not serializable.
Thanks mate, though it is a late reply, appreciated your help. :) @Abubakar
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.

2 Comments

Are you sure that you are able to get HashMap<String, String> using getExtras.get("key") ?
yeah...as long as you passed "HashMap<String,String>" to the receiving activity
0

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.

2 Comments

thanx for the support, and "new" information , but i just need to pass HashMap between two activities i.e. From activity A to activity B
I figured. Just wanted to offer it as an alternative.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.