0

I'm trying to pass Hashmap to another intent but I get error:

Cannot resolve method 'putExtra(java.lang.String,java.util.Map<String,android.content.pm.ApplicationInfo>

Code:

Map<String, ApplicationInfo> map = returnedMap;
Intent i = new Intent(LoadingScreen.this, DisplayClass.class);
i.putExtra("total",total);
i.putExtra("map", map);
startActivity(i);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

3 Answers3

0

As HashMaps serialize and Maps don't, so try doing the below segment because you are using using the

Intent.putExtra(String, Serializable) method:

HashMap<String, ApplicationInfo> hashMap = returnedMap;
Suvansh
  • 266
  • 1
  • 6
0

You need to create a HashMap cause HashMap is the class which implements Serializable. And Map is a parent interface of HashMap.

HashMap<String, ApplicationInfo> map = returnedMap;
Intent i = new Intent(LoadingScreen.this, DisplayClass.class);
i.putExtra("total",total);
i.putExtra("map", map);
startActivity(i);
ADM
  • 20,406
  • 11
  • 52
  • 83
0

Intent.putExtra only accepts primitive type / Parcelable, Serializable object.

Since you're holding a Map, it's pretty easy to send it as HashMap because HashMap implements Serializable interface.

// If your originial Map is not a HashMap, you have to convert it
// HashMap<String, ApplicationInfo> map = new HashMap<>(returnedMap);
// If it's already an HashMap, you have to cast it
// HashMap<String, ApplicationInfo> map = (HashMap)returnedMap;
i.putExtra("map", map);

And in the receiver side:

HashMap<String, ApplicationInfo> map = (HashMap<>)intent.getSerializableExtra("map")
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51