Link between tabs SlidingTabLayout

I searched a lot how to communicate between fragments using SlidingTabLayout, but actually did not get a good answer. I know using an ActionBar, but I wanted a new way for android lollipop using SlidingTabLayout. I tried this -> http://android-er.blogspot.in/2012/06/communication-between-fragments-in.html , but I need a material design. I referred to this link http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html for creating sliding tabs of materials. Now I wanted to know how to communicate between sliding tabs . I tried a lot, but could not find the answer I was looking for. Any help would really be liked.

+7
android android-fragments material-design
source share
3 answers

The cleanest way is to define the interface that the Activity containing the Fragment will implement. Here is how I recently solved this:

First, define the interface in its own file, because it must 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 Fragment you can get the hosting of the Activity using getActivity() , and to use the communication methods just enter the activity into the implemented communication interface as follows:

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

EDIT . To pass a message to other Fragment

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

Then implement this on your tabs

 public class Tab1 extends Fragment implements ReceiverInterface { .... code ..... public void receiveString(String str) { //use str } } 

To send this message to other fragments, activity is required to see them. For example, now change printMessage() , which Activity implements 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); } } 
+4
source share

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

As you mentioned earlier, you tried this , so I will go with different Fragments .

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

Add it to your build.gradle dependencies inside the 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 can also achieve this using Intents .

1 - Create a class to represent the event when the 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: fooobar.com/questions/544321 / ...

+2
source share

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

0
source share

All Articles