Anuj
Anuj

Reputation: 129

How to use bundle to send data from one fragments to another

I have 3 fragments A,B,C.In fragment A i have fields say name and password.In fragment B i have fields contact and country.And in C i have fields like summary.Now in fragment C i am calling a web service which is having all the parameters from A and B.So in A i have stored fields in bundle.Now in fragment B i have to get the bundle from A and make a new to bundle and include fields from A and B in that bundle and use that in C.My question is that ,cant i use the bundle from A and B directly in C ????

Upvotes: 0

Views: 1143

Answers (3)

fbsall
fbsall

Reputation: 21

Hi this is how How to use bundle to send data from one fragments to another

Frament A

Bundle bundle = new Bundle;
bundle.putString(Const.BUNDLE_KEY1, value1);
bundle.putString(Const.BUNDLE_KEY2, value2);
Fragment fragmentB = new FragmentB();
fragmentB.setArguments(bundle);

//then replace the fragment here

Fragment B public class FragmentB extends Fragment {

public static FragmentB newInstance(Bundle bundle) {
    FragmentB fragmentB= new PVResultatFragment();
    Bundle args = new Bundle();
    args.putString(Const.BUNDLE_KEY1, value1);
    args.putString(Const.BUNDLE_KEY2, value2);
    pvf.setArguments(args);
    return fragmentB;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_b, container, false);
    resolver = getActivity().getContentResolver();
    Bundle bundle = getArguments();
    String value1 = bundle.getString(Const.BUNDLE_KEY1);
    String value2 = bundle.getString(Const.BUNDLE_KEY2);

...

Upvotes: 2

joselufo
joselufo

Reputation: 3415

Yes you can do, to pass data between fragments of the solutions is to do it through the parent activity.

Example code:

Main Activity

    private Bundle  dataBetweenFragment; //Global variable

Now implements this method for you can to access of data.

    public void saveData (Bundle data) {
          this.dataBetweenFragment = data;
    }

    public Bundle getSavedData () {
          return this.dataBetweenFragment;
    }

Fragment A or B or C or any:

    private MainActivity activity;

    public void onAttach(Activity activity) {
            this.activity = (MainActivity) activity;        
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {   
          super.onCreate(savedInstanceState);   
          /**Get data from Activity **/
          Bundle data = mainActivity.getSavedData();
          String dataString = data.getString("data");
    }


    public void sendDataOtherFragment () {
          Bundle data = new Bundle();
          data.putString("data", "Hi!");
          this.mainActivity.saveData(data);
    }

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

To pass a bundle to a fragment, use:

fragment.setArguments(bndl);

and to use that bundle in the referring fragment, use:

Bundle bndl = fragment.getArguments();

The is one of the simplest and quickest way.

Upvotes: 2

Related Questions