Send data from activity to fragment

后端 未结 2 1736
庸人自扰
庸人自扰 2021-01-26 12:03

How can we send data from actvity to fragment? The Fragments are configured to actvity by using FragmentPagerAdapter.

Regards mini.

2条回答
  •  感动是毒
    2021-01-26 12:06

    You can perform this by using Bundle

    Send data from the activity (or fragment) :

    int a = 5;
    
    Bundle args = new Bundle(); 
    args.putInt("INT_DATA_TAG", a); 
    
    Fragment fragment = Fragment.newInstance(args); 
    //Making fragment transaction 
    

    Retrieve data in the fragment

    int a;
    public static Fragment newInstance(Bundle args) {
          a = args.getInt("INT_DATA_TAG"); //use a constant for the tag
          return new Fragment();
    }
    

提交回复
热议问题