Send data from activity to fragment

后端 未结 2 1714
庸人自扰
庸人自扰 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();
    }
    
    0 讨论(0)
  • 2021-01-26 12:31
    • You can pass a bundle to the fragment on creation with setArguments.
    • You can create methods to set the data on the Fragment class.
    0 讨论(0)
提交回复
热议问题