How to use Parcelable in fragment for getting data?

前端 未结 1 1835
灰色年华
灰色年华 2021-01-01 19:21

I can use Parcelable in Activity but I don\'t know how to use it in Fragment.

I have a ListFragment in FragmentGet to display all rows database in ListView, I want t

相关标签:
1条回答
  • 2021-01-01 19:24

    Passing data to Fragments is carried by using Bundles but not Intents.

    In FragmentSend change

    Intent intent = new Intent(getContext(), FragmentGet.class);
    intent.putExtra("Student", model);
    
    Fragment fragmentGet = new FragmentGet();
    fragmentGet.setArguments(intent.getExtras());
    

    to this

    Fragment fragmentGet = new FragmentGet();
    Bundle bundle = new Bundle();
    bundle.putParcelable("Student", model);
    fragmentGet.setArguments(bundle);
    

    And to receive the data in FragmentGet change

    Bundle bundle = getActivity().getIntent().getExtras();
    model = bundle.getParcelable("Student");
    

    to

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        model = bundle.getParcelable("Student");
    }
    
    0 讨论(0)
提交回复
热议问题