4

I have a HashMap(String,HashMap(String,Object)) in one of my activity. How do I send this HashMap to another activity via intents

How to add this HashMap to intent extras?

ranjith Gampa
  • 105
  • 2
  • 12
  • 1
    You can find the answer in here: http://stackoverflow.com/a/4992465/4287861 – Ran Hassid Jul 31 '16 at 14:51
  • The comments and answers here show you how to pass the HashMap through an Intent, but you might want to take this opportunity to ask yourself whether you could design your app in a better way. Whenever you "need" to pass a Serializable between Activities, IMHO, it indicates that you may have a weak "model", or none at all. Non-trivial apps should avoid just passing data from Activity to Activity, and implement a model that is separate from the UI, and then inject that model into Activities where needed. See also the MVC or MVP design pattern. – GreyBeardedGeek Jul 31 '16 at 15:49

3 Answers3

3

Sending HashMap

HashMap<String, Object> hashmapobject =new HashMap<>();
HashMap<String, Object> newhashMap=new HashMap<>();
hashmapobject.put("key",newhashMap);

Intent intent = new Intent(SenderActivity.this, NextActivity.class);
intent.putExtra("hashMapKey", hashmapobject);
startActivity(intent);

Receiving HashMap

Intent intent = getIntent();    
HashMap<String, Object> hashMapObject = (HashMap<String, Object>)       intent.getSerializableExtra("hashMapKey");
HashMap<String, Object> newhashMap=(HashMap<String, Object>)hashMapObject.get("key");
Amit Yadav
  • 406
  • 2
  • 11
  • 2
    You need to use putSerializable() instead of putExtra() - http://stackoverflow.com/questions/11452859/android-hashmap-in-bundle – Vaiden Jul 31 '16 at 15:08
0

There could be multiple approaches to your problem and each one depends on what datatype you are storing in map. Easiest approach is to use JSONObject from json.org pass it as String and while receiving convert it back to JSON. JSONObject uses a LinkedHashMap inside thus you will be able to use the features of HashMap.

JSONObject obj=new JSONObject();
obj.put("key1", "value1");
obj.put("key2", "value2");
obj.put("key3", "value3");
.
.
.
obj.put("keyN", "valueN");

intent.putExtra("map", obj.toString());

While receiving

JSONObject obj=new JSONObject(getIntent().getStringExtra("map"));

For complex datatypes try considering either JSON Libraries like GSON or use Parcelable interface.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
  • 1
    You can just pass the HashMap as it is without parsing it to JSON.. HashMap implements Serializable interface by default so you can just use: intent.putExtra("map",HashMap<>) and to read it: getIntent().getSerializableExtra("map") – Ran Hassid Jul 31 '16 at 15:05
  • I updated my answer Ran – Nayan Srivastava Jul 31 '16 at 15:06
0

Hi you can use Parcelable :

Write class like this :

public class Person implements Parcelable {
String name;
int age;
Date brithDate;

public Person(String name, int age, Date brithDate) {
    this.name = name;
    this.age = age;
    this.brithDate = brithDate;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.name);
    dest.writeInt(this.age);
    dest.writeLong(brithDate != null ? brithDate.getTime() : -1);
}

protected Person(Parcel in) {
    this.name = in.readString();
    this.age = in.readInt();
    long tmpbrithDate = in.readLong();
    this.brithDate = tmpbrithDate == -1 ? null : new Date(tmpbrithDate);
}

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
    public Persona createFromParcel(Parcel source) {
        return new Person(source);
    }

    public Person[] newArray(int size) {
        return new Person[size];
    }
};

Put extra :

intent.putExtra("person", new Person("Ahmad",34,date));

Get :

Bundle data = getIntent().getExtras();
Person person= (Person) data.getParcelable("person");

OR you can copy class this site and convert to Parcelable class :

http://www.parcelabler.com/

OR you can use this library hrisey

https://github.com/mg6maciej/hrisey/wiki/Parcelable#details

Or you can use Android Studio have plugins for this:

  • Android Parcelable code generator (Apache License 2.0)
  • Auto Parcel (The MIT License)
  • SerializableParcelable Generator (The MIT License)
  • Parcelable Code Generator (for Kotlin) (Apache License 2.0)
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98