MVK059
MVK059

Reputation: 351

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 android lollipop using SlidingTabLayout. I tried this-> http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html but I wanted material design. I referred this link http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html for making material design sliding tabs. Now I wanted to know how to communicate between the sliding tabs. I've tried a lot but couldn't find the answer I was looking for. Any help would really appreciated.

Upvotes: 8

Views: 2281

Answers (3)

milez
milez

Reputation: 2201

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.

public interface FragmentCommunication
{
    public void printMessage(String message);
    //....    
}

In your Activity you need to implement this interface

public class MainActivity extends ActionBarActivity implements FragmentCommunication
{   
    //....
    public void printMessage(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

public Interface ReceiverInterface
{
    public void receiveMessage(String str);
}

Then implement this in your tabs

public class Tab1 extends Fragment implements ReceiverInterface
{
   // .... code .....
    public void receiveString(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);
        }
    } 

Upvotes: 6

Dan
Dan

Reputation: 1174

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

Upvotes: 0

Machado
Machado

Reputation: 14499

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:

public class TextChangedEvent {
  public String newText;
  public TextChangedEvent(String newText) {
      this.newText = newText;
  }
}

2 - Fragment A:

//when text changes
EventBus bus = EventBus.getDefault();
bus.post(new TextChangedEvent(newText));    

3 - Fragment B:

EventBus bus = EventBus.getDefault();

//Register to EventBus
@Override
public void onCreate(SavedInstanceState savedState) {
 bus.register(this);
}

//catch Event from fragment A
public void onEvent(TextChangedEvent event) {
 yourTextView.setText(event.newText);
}

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

Upvotes: 4

Related Questions