Skip to content Skip to sidebar Skip to footer

Communication Between Slidingtablayout Tabs

I've searched a lot on how to communicate between fragments using SlidingTabLayout but haven't really got a good answer. I know using ActionBar but I wanted the new way that is for

Solution 1:

The cleanest way is to define an interface that the Activity that contains the Fragments will implement. This is how I recently solved this:

First define the interface in it's own file, because it has to be visible to other classes.

publicinterfaceFragmentCommunication
{
    publicvoidprintMessage(String message);
    //....    
}

In your Activity you need to implement this interface

publicclassMainActivityextendsActionBarActivityimplementsFragmentCommunication
{   
    //....publicvoidprintMessage(String message)
    {
        System.out.println(message);
    }
}

Finally in your Fragments you can get the hosting Activity with getActivity() and to use the communication methods just cast the activity into the implemented communication interface like so:

((FragmentCommunication) getActivity()).printMessage("Hello from Fragment!");

EDIT: To further pass the message to other Fragments do this: since your tabs all extend Fragment it is the best to create another interface

publicInterfaceReceiverInterface{
    publicvoid receiveMessage(String str);
}

Then implement this in your tabs

publicclassTab1extendsFragmentimplementsReceiverInterface
{
   // .... code .....publicvoidreceiveString(String str)
    {
        //use str
    }
}

To further send this message to other Fragments it is required that the activity sees them. For example now modify the printMessage() that Activity implements to this

    public void printMessage(String message)
    {
        System.out.println(message);
        //Send the message that came from one fragment to another
        if (tabFragment1 instanceof ReceiverInterface){
            ((ReceiverInterface) tabFragment1).receiveMessage(message);
        }
    } 

Solution 2:

When you slide tabs (ViewPager), you can either work with the same Fragment or use different Fragments.

As you previously mentioned, you tried this, so I'm going with different Fragments.

What you are going to do is basically use EventBus: https://github.com/greenrobot/EventBus.

Add it to your build.gradle dependencies located inside app folder.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile'com.android.support:appcompat-v7:22.1.1'compile'de.greenrobot:eventbus:2.4.0'   
}

You could also achieve it by using Intents.

1 - Create a class to represent event when your text changes:

publicclassTextChangedEvent {
  publicString newText;
  publicTextChangedEvent(String newText) {
      this.newText = newText;
  }
}

2 - Fragment A:

//when text changesEventBusbus= EventBus.getDefault();
bus.post(newTextChangedEvent(newText));    

3 - Fragment B:

EventBus bus = EventBus.getDefault();

//Register to EventBus@OverridepublicvoidonCreate(SavedInstanceState savedState) {
 bus.register(this);
}

//catch Event from fragment ApublicvoidonEvent(TextChangedEvent event) {
 yourTextView.setText(event.newText);
}

Source: https://stackoverflow.com/a/20475430/1549700

Solution 3:

Use EventBus GitHub library. This is currently the simplest and most convenient way. https://github.com/greenrobot/EventBus

Post a Comment for "Communication Between Slidingtablayout Tabs"