Passing an Object from an Activity to a Fragment

前端 未结 8 2088
别那么骄傲
别那么骄傲 2020-11-27 10:55

I have an Activity which uses a Fragment. I simply want to pass an object from this Activity to the Fragment.

How

8条回答
  •  长情又很酷
    2020-11-27 11:28

    To pass an object to a fragment, do the following:

    First store the objects in Bundle, don't forget to put implements serializable in class.

                CategoryRowFragment fragment = new CategoryRowFragment();
    
                // pass arguments to fragment
                Bundle bundle = new Bundle();
    
                // event list we want to populate
                bundle.putSerializable("eventsList", eventsList);
    
                // the description of the row
                bundle.putSerializable("categoryRow", categoryRow);
    
                fragment.setArguments(bundle);
    

    Then retrieve bundles in Fragment

           // events that will be populated in this row
            mEventsList = (ArrayList)getArguments().getSerializable("eventsList");
    
            // description of events to be populated in this row
            mCategoryRow = (CategoryRow)getArguments().getSerializable("categoryRow");

提交回复
热议问题