android - Pass data from Activity to Fragment in ViewPager

后端 未结 6 560
夕颜
夕颜 2020-12-29 07:41

I have a simple Activity with TabLayout, ViewPager and 2 Fragments in it. Here\'s my Activity:

public class ManagementCompanyOverviewActivity extends BaseAct         


        
6条回答
  •  情话喂你
    2020-12-29 08:07

    From Activity or ViewPager

    Bundle bundle = new Bundle();
    bundle.putParcelable(Keys.KEY_ITEM, cardResult);
    CardItemFragment cardItemFragment = new CardItemFragment();
    cardItemFragment.setArguments(bundle);
    

    From Fragment

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         getBundle();
    }
    
    
    private void getBundle() {
        bundle = getArguments();
        if (null != bundle) {
            if (bundle.containsKey(Keys.KEY_ITEM)) {
                cardResult = bundle.getParcelable(Keys.KEY_ITEM);
            }
        }
    }
    

提交回复
热议问题