2

I am trying to pass HashMap through arguments .But I am getting empty hashmap while getting it through getSeralizable method(Hashmap that I am putting is not empty & having a size of 11). The following is my code snippet.

public static FirstPageFragment newInstance(boolean is_pending, HashMap<String, Object> result_map) {
    FirstPageFragment fragment = new FirstPageFragment();
    Bundle args = new Bundle();
    args.putBoolean(ARG_PARAM1, is_pending);
    args.putSerializable(ARG_PARAM2, result_map);
    fragment.setArguments(args);
    return fragment;
}

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        Bundle b=getArguments();
        isPending = b.getBoolean(ARG_PARAM1);
        mResultMap = (HashMap<String, Object>)b.getSerializable(ARG_PARAM2);
    }

}

Can we do like this? If it can't be done like this, is there any alternate way to do this.

Anu
  • 1,303
  • 3
  • 23
  • 38

3 Answers3

1

try this:

Bundle extras = new Bundle();
extras.putSerializable("YourHashMap",hashMap);
intent.putExtras(extras);

and in the other Activity

Bundle bundle = this.getIntent().getExtras();

if(bundle != null) {
   hashMap = bundle.getSerializable("YourHashMap");
}

https://developer.android.com/reference/java/util/HashMap.html

HashMap ___

extends AbstractMap implements Map, Cloneable, Serializable

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • It's working in activity. But is it possible to set the bundle as arguments in fragment? – Anu Jan 18 '17 at 12:12
  • @Anu here http://stackoverflow.com/questions/9931993/passing-an-object-from-an-activity-to-a-fragment – Charuක Jan 18 '17 at 12:19
0

You can do it like this as HashMap implementation is serializable

gilgil28
  • 540
  • 5
  • 12
0

Use putExtra(String key, Serializable obj) to insert the HashMap and on the other Activity use getIntent().getSerializableExtra(String key), You will need to Cast the return value as a HashMap though.

Vishal Jadav
  • 924
  • 9
  • 11