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.

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

It's working in activity. But is it possible to set the bundle as arguments in fragment?
0

You can do it like this as HashMap implementation is serializable

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.