4

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

I don't want to pass data between fragments using activity like this : Communicating with Other Fragments

Below is my scenario :

I have one Parent fragment and inside that there are two child fragments.Now my need is to pass data between these two fragments.How to achieve this?

I looked into this : Event Bus but not getting working example for fragments.

Is there any other alternative to pass data between fragments?

Any help will be appreciated.

Edited as per InnocentKiller's answer :

In FragmentOne , I have implemented :

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

In FragmentTwo, I have implemented :

    Bundle bundle = this.getArguments();
    String myInt = bundle.getString("Hello","Test");
    mStartTripButton.setText(myInt);
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
  • Without Activity How can you manage Fragment? – M D Dec 15 '14 at 12:36
  • You can use Singleton/static variable – Suvitruf - Andrei Apanasik Dec 15 '14 at 12:37
  • @M D : In my parent fragment, i have a layout file which has two fragments.My parent fragment is called when I click on navigation drawer.And my navigation drawer is main activity. – Siddharth_Vyas Dec 15 '14 at 12:39
  • @M D : I dont want a call back method on my main activity, i want it inside my parent fragment. And doing this shows ClassCast exception. – Siddharth_Vyas Dec 15 '14 at 12:40
  • Regarding EventBus, you can use the example from my answer directly. That is: in frag A `EventBus.getDefault().postSticky("Hello")` and in frag B `String hello = EventBus.getDefault().getStickyEvent(String.class)` – cYrixmorten Dec 15 '14 at 16:19
  • Or take a look at this: http://stackoverflow.com/questions/20838726/how-to-use-greenrobot-to-pass-data-to-activity-or-fragment-that-has-not-been-ini – cYrixmorten Dec 15 '14 at 16:29
  • And how to pass data from FragmentOne to FragmentTwo when the data is received from server? You haven't access to data when you initiate FragmentTwo. – Milad Faridnia Dec 03 '17 at 07:31

3 Answers3

17

Best Way to exchange data between activity/fragments, fragment/fragment/, activity/activity, class/ class, make a common singleton class like:

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

now set from anywhere(Fragment, activity, class) at any event before you move to new screen

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

now get anywhere(Fragment, activity, class)

 String _data = DataHolderClass.getInstance().getDistributor_id();
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • How reliable is this method? As far as I know, there's no guarantee that a static var will retain its value. Looks very convenient though. – jdkraftz Apr 19 '15 at 19:14
  • 1
    @jdkraftz I have not dug up the reference but I am 99% sure that the static variable is kept during the application lifecycle. At least I have been using this approach in an app that is continuously used 8 hours per day and have had no problems so far. – cYrixmorten May 20 '15 at 14:02
8

You can use EventBus https://github.com/greenrobot/EventBus to pass any complex object around in Android.

Example passing an object from one Activity to another:

Add your object on the EventBus:

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

Anywhere in SomeActivity:

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

This examle is just to show that it works across Activities, you can call getStickyEvent to get anyObject anywhere in your code.

So, you can post an object in Fragment A, and use getStickyEvent in fragment B to retrieve it.

This is especially convenient if you have a lot of arguments and/or complex objects you wish to move from one place to the other. Simply create a single 'holder' object containing getter/setters for the arguments and then pass it along. Much simpler than having to pass them separately and somehow serialize the complex objects.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • +1 for a globally usable library. But it is a bit heavy for this task alone. Building your own data holder is lighter. – C-- Mar 03 '15 at 10:54
  • 1
    @SubinSebastian You are right, actually recently transitioned to temporal static holders in many cases. EventBus though offers other very nice functionality, such as broadcasting complex objects. Suddenly it is easy to, for instance, update UI based on service events. Even if wanting to communicate non trivial data. – cYrixmorten Mar 03 '15 at 12:15
  • plus it is not suitable at all for this! You can not pass data from a fragment to another without engaging the host activity which is awkward! – Nasz Njoka Sr. Sep 07 '16 at 11:09
  • It may be awkward, but you can, either with this method or static holders as described by @De – cYrixmorten Sep 07 '16 at 12:11
4

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

Use a Bundle, So write below code in first fragment from where you want to send data.

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

and to retrieve that data, use the following code in your other fragment

String strtext=getArguments().getString("message");
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84