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?
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?
 
    
    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");
 
    
    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.
 
    
    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 :
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:
