1

Can anyone help me to send a hashmap through a intent and receive it on the other activity first parameter is strings and 2nd parameter is a list of strings cant find anyone that is struggling or trying to send this through a intent the way that i am trying too

Map<String, List<String>> map = new hashmap<>();
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
The43Joker
  • 52
  • 1
  • 7
  • you can use serializeable for this purpose – Ali Ahmed Nov 06 '18 at 06:56
  • 5
    Possible duplicate of [Android - How to pass HashMap between activities?](https://stackoverflow.com/questions/4992097/android-how-to-pass-hashmapstring-string-between-activities) – Kunu Nov 06 '18 at 07:02
  • Kunu sending a HashMap is different than sending a HashMap> AliAhmed Thank you good sir your answer did work – The43Joker Nov 06 '18 at 07:57

2 Answers2

4

You will have to use Serializable and pass the map through intent. Code Example of Data Sender is as follows :

Map map = new HashMap<String,List<String>>();

List<String> l1 = new ArrayList();

l1.add("HEllo");
l1.add("John");
l1.add("Michael");
l1.add("Jessy");

map.put("Names" , l1);

Intent intent = new Intent("CurrentActivityName".this, "DestinationActivityName".class);

intent.putExtra("Map",(Serializable) map);

startActivity(intent);

Code for Receiver:

Map map = new HashMap<String,List>();
map = (Map) getIntent().getSerializableExtra("Map");

Now you can access the data using variable named map.

ZubairHasan
  • 165
  • 1
  • 8
1

Create a Model Class that implements Serializable

public class DataWrapper implements Serializable {
   private Map map;

   public DataWrapper(Map dataMap) {
      this.map= dataMap;
   }

   public Map getData() {
       return this.map;
   }

}

For Fragment

    Fragmentt recent = new Fragmentt();
    Bundle bundle = new Bundle();
    Map m = new HashMap<>();
    m.put("data", data);
    bundle.putSerializable("Data", new DataWrapper(m));
    recent.setArguments(bundle);

Receive Data on Next Fragment

        DataWrapper dataWrapper = (DataWrapper) bundle.getSerializable("Data");
        map = dataWrapper.getData();

For Activity

        Intent intent = new Intent(this, Activity.class);
        Map map = new HashMap<>();
        map.put("Data", data);
        intent.putExtra("Data", new DataWrapper(map));
        startActivity(intent);

Receive Data on Next Activity

        Map map;
        DataWrapper dataWrapper = (DataWrapper) getIntent().getSerializableExtra("Data");
        map = dataWrapper.getData();
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19