简体   繁体   中英

Send data from activity to fragment

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

Regards mini.

  • You can pass a bundle to the fragment on creation with setArguments .
  • You can create methods to set the data on the Fragment class.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM