Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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

Solution 2:

You can perform this by using Bundle

Send data from the activity (or fragment) :

inta=5;

Bundleargs=newBundle(); 
args.putInt("INT_DATA_TAG", a); 

Fragmentfragment= Fragment.newInstance(args); 
//Making fragment transaction 

Retrieve data in the fragment

int a;
publicstatic Fragment newInstance(Bundle args){
      a = args.getInt("INT_DATA_TAG"); //use a constant for the tagreturnnewFragment();
}

Post a Comment for "Send Data From Activity To Fragment"