Passing objects in to Fragments

后端 未结 2 669
醉酒成梦
醉酒成梦 2020-12-05 02:10

I\'ve been working with lots of Fragments recently and have been using two distinct methods of passing in objects to the Fragments, but the only difference that

2条回答
  •  猫巷女王i
    2020-12-05 02:45

    for Collection such as List :

    I wanted to share my experience.

    you need to implement Parcelable

    Just use the putParcelableArrayList method.

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

    And retrieve it using...

    localities = savedInstanceState.getParcelableArrayList(KEY_LCLass_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.

提交回复
热议问题