Transferring data between two fragments without using activity

I want to transfer data between two fragments without using the activity and fragment activity.

I do not want to transfer data between fragments using the following actions: Communicating with other fragments

Below is my scenario:

I have one parent fragment and inside it there are two child fragments. Now I need to transfer data between these two fragments. How to do it?

I reviewed this: Event Bus , but not getting a working example for fragments.

Is there any other alternative for transferring data between fragments?

Any help would be appreciated.

Edited as InnocentKiller answer:

In FragmentOne, I implemented:

    FragmentTwo = new FragmentTwo();
    Bundle bundle = new Bundle();
    bundle.putString("Hello", "My name is Siddharth");
    fragment.setArguments(bundle);

In FragmentTwo, I implemented:

    Bundle bundle = this.getArguments();
    String myInt = bundle.getString("Hello","Test");
    mStartTripButton.setText(myInt);
+4
3

/, //, /, /, , :

public class DataHolderClass {
private static DataHolderClass dataObject = null;

private DataHolderClass() {
    // left blank intentionally
}

public static DataHolderClass getInstance() {
    if (dataObject == null)
        dataObject = new DataHolderClass();
    return dataObject;
}
private String distributor_id;;

 public String getDistributor_id() {
    return distributor_id;
 }

 public void setDistributor_id(String distributor_id) {
    this.distributor_id = distributor_id;
 }
}

(, , ) ,

DataHolderClass.getInstance().setDistributor_id("your data");

(, , )

 String _data = DataHolderClass.getInstance().getDistributor_id();
+15

EventBus https://github.com/greenrobot/EventBus Android.

:

EventBus:

EventBus.getDefault().postSticky(anyObject);
startActivity(new Intent(getActivity(), SomeActivity.class));

Anywhere in SomeActivity:

AnyObject anyObject = EventBus.getDefault().getStickyEvent(AnyObject.class);

- , Action, getStickyEvent, anyObject .

, A getStickyEvent B .

, / , . "", / , . , - .

+8

I think you are trying to transfer data from one fragment to another fragment. So try using the code below.

Use the package, so write the code below in the first fragment where you want to send data from.

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("message", "From Activity");
fragment.setArguments(bundle);

and to extract this data use the following code in another snippet

String strtext=getArguments().getString("message");
+3
source

All Articles