Tab Fragment Button Click Send Data To More Than One Tabfragment
i have three tab fragments TAB1,TAB2 and TAB3. On TAB1 there is a button when when clicked it has to send data to TAB2 and TAB3 using interface and display data in the textView in
Solution 1:
In your Tab1ToTab3 method you're instanciating TabFragment3 with your second fragment :
TabFragment3fragment= (TabFragment3) adapter.getFragment(1);
replace it with
TabFragment3fragment= (TabFragment3) adapter.getFragment(2);
Solution 2:
// Update your getItem() method with this
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
Bundlebundle=newBundle();
bundle.putString("YOUR_KEY","YOUR VALUE");
TabFragment1tab1=newTabFragment1();
tab1.setArguments(bundle);
return tab1;
case1:
Bundlebundle2=newBundle();
bundle2.putString("YOUR_KEY","YOUR VALUE");
TabFragment2tab2=newTabFragment2();
tab2.setArguments(bundle2);
return tab2;
case2:
Bundlebundle3=newBundle();
bundle3.putString("YOUR_KEY","YOUR VALUE");
TabFragment3tab3=newTabFragment3();
tab3.setArguments(bundle3);
return tab3;
case3:
Bundlebundle4=newBundle();
bundle4.putString("YOUR_KEY","YOUR VALUE");
TabFragment4tab4=newTabFragment4();
tab4.setArguments(bundle4);
return tab4;
default:
returnnull;
}
// Under TabFragment1, TabFragment2, TabFragment3, TabFragment4 Override onActivityCreated() to get your data String and setMenuVisibility(boolean menuVisible) to update view
publicstaticString data;
@OverridepublicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
String yourValue = bundle.getString("YOUR_KEY");
imageView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
TabFragment1.data="Update";
TabFragment2.data="Update";
TabFragment3.data="Update";
TabFragment4.data="Update";
//update your current View from here
}
});
}
@OverridepublicvoidsetMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if(menuVisible){
//update your current View from here
}
}
by using this you can update all fragments data view from any fragment. click
Post a Comment for "Tab Fragment Button Click Send Data To More Than One Tabfragment"