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.
These methods have a features: always attaching the "tab widget" with a ViewPager , and in order to make this requirement, we must disable swipe feature of the ViewPager . Now, with Material design, we now use TabLayout widget, which can "stand alone" to build a tab bar and do not need a ViewPager anymore.
The cleanest way is to define an interface that the Activity
that contains the Fragment
s 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 Fragment
s 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 Fragment
s 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);
}
}
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
.
public class TextChangedEvent {
public String newText;
public TextChangedEvent(String newText) {
this.newText = newText;
}
}
//when text changes
EventBus bus = EventBus.getDefault();
bus.post(new TextChangedEvent(newText));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With