Search code examples
androidfragmentbundle

How to put List<HashMap> into Bundle?


In my Fragment, there is a

List<HashMap> movieList

How could I store movieList into Bundle in

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);


}

Thank you.


Solution

  • You can try this way...

    HashMap<String, String> contactsMap = new HashMap<>();
    contactsMap.put("key1" , "value1");
    contactsMap.put("key2" , "value2");
    
    ArrayList<HashMap> list = new ArrayList<>();
    list.add(contactsMap);
    
    Bundle b = new Bundle();
    b.putSerializable("HASHMAP", list);
    
    ArrayList<HashMap> hashmapList = (ArrayList<HashMap>)            b.getSerializable("HASHMAP");
    HashMap<String, String> map = hashmapList.get(0);
    
    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey()+" : "+entry.getValue());
    }
    

    You will get the same data. Make sure your movie model class is serializable.