Android ArrayList pass as parcelable

后端 未结 3 1092
长发绾君心
长发绾君心 2020-12-03 07:08

Code now modified to reflect the accepted solution.

This now serves as a working example of how to pass a custom ArrayList into a DialogFrag

3条回答
  •  执笔经年
    2020-12-03 07:43

    I know this question is rather old but since I originally came here looking for answers, I wanted to share my experience.

    Yes, you need to implement Parcelable for your Locality class but that is it.

    If your LocalityList is ONLY a wrapper for ArrayList, then you do not need it.

    Just use the putParcelableArrayList method.

    ArrayList localities = new ArrayList;
    ...
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList(KEY_LOCALITY_LIST, localities);
    fragmentInstance.setArguments(bundle);
    
    return fragmentInstance;
    

    And retrieve it using...

    localities = savedInstanceState.getParcelableArrayList(KEY_LOCALITY_LIST);
    

    So, unless you need the custom ArrayList for some other reason, you can avoid doing any of that extra work and only implement Parcelable for your Locality class.

提交回复
热议问题