86619

Tab Fragment Button Click send data to more than one tabFragment

Question:

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 the respective fragment. So far i have managed to send from TAB1 to TAB2,like below

// interface codes public interface IFragmentToActivity { void Tab1ToTab2(String value); } // in activity @Override public void Tab1ToTab2(String value) { TabFragment2 fragment = (TabFragment2) adapter.getFragment(1); if (fragment != null) { fragment.fragmentCommunicationFragment(value); } else { // Log.i(LOG_TAG, "Fragment 2 is not initialized"); } } /// in TAB1 i send data @Override public void onClick(View v) { switch (v.getId()) { case R.id.button2: mCallback.FragmentToFragment("hi from tab 1"); break; } }

how can i make it work for all fragments? this is what i have tried when i want to send to TAB2 and 3.

interface codes

public interface IFragmentToActivity { void Tab1ToTab2(String value); void Tab1ToTab3(String value); }

TAB1 send data

@Override public void onClick(View v) { switch (v.getId()) { case R.id.button2: mCallback.Tab1ToTab2("hi from tab 1"); mCallback.Tab1ToTab3(hi from tab 1"); break; }

codes in Main Activity to receive

@Override public void Tab1ToTab2(String value) { TabFragment2 fragment = (TabFragment2) adapter.getFragment(1); if (fragment != null) { fragment.fragmentCommunicationFragment(value); } else { // Log.i(LOG_TAG, "Fragment 2 is not initialized"); } } @Override public void Tab1ToTab3(String value) { TabFragment3 fragment = (TabFragment3) adapter.getFragment(1); if (fragment != null) { fragment.FromFragmentOne(value); } else { // Log.i(LOG_TAG, "Fragment 3 is not initialized"); } }

/// TAB 2 method receive data

public void fragmentCommunicationFragment(String value){ mTextView1.setText(value); }

/// TAB 3 method receive data

public void FromFragmentOne(String kabila){ mTextView1.setText(kabila); }

// adapter

public class PagerAdapter extends FragmentStatePagerAdapter { private final SparseArray<WeakReference<Fragment>> instantiatedFragments = new SparseArray<>(); private ArrayList<String> mTabHeader; int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Override public Fragment getItem(int position) { switch (position) { case 0: TabFragment1 tab1 = new TabFragment1(); return tab1; case 1: TabFragment2 tab2 = new TabFragment2(); return tab2; case 2: TabFragment3 tab3 = new TabFragment3(); return tab3; case 3: TabFragment4 tab4 = new TabFragment4(); return tab4; default: return null; } }

Answer1:

In your Tab1ToTab3 method you're instanciating TabFragment3 with your second fragment :

TabFragment3 fragment = (TabFragment3) adapter.getFragment(1);

replace it with

TabFragment3 fragment = (TabFragment3) adapter.getFragment(2);

Answer2:

<blockquote>

// Update your getItem() method with this

</blockquote> @Override public Fragment getItem(int position) { switch (position) { case 0: Bundle bundle = new Bundle(); bundle.putString("YOUR_KEY","YOUR VALUE"); TabFragment1 tab1 = new TabFragment1(); tab1.setArguments(bundle); return tab1; case 1: Bundle bundle2 = new Bundle(); bundle2.putString("YOUR_KEY","YOUR VALUE"); TabFragment2 tab2 = new TabFragment2(); tab2.setArguments(bundle2); return tab2; case 2: Bundle bundle3 = new Bundle(); bundle3.putString("YOUR_KEY","YOUR VALUE"); TabFragment3 tab3 = new TabFragment3(); tab3.setArguments(bundle3); return tab3; case 3: Bundle bundle4 = new Bundle(); bundle4.putString("YOUR_KEY","YOUR VALUE"); TabFragment4 tab4 = new TabFragment4(); tab4.setArguments(bundle4); return tab4; default: return null; } <blockquote>

// Under TabFragment1, TabFragment2, TabFragment3, TabFragment4 Override onActivityCreated() to get your data String and setMenuVisibility(boolean menuVisible) to update view

</blockquote> public static String data; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); String yourValue = bundle.getString("YOUR_KEY"); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TabFragment1.data="Update"; TabFragment2.data="Update"; TabFragment3.data="Update"; TabFragment4.data="Update"; //update your current View from here } }); } @Override public void setMenuVisibility(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

Recommend

  • Access ViewPager fragment from Host activity returns fragment as NULL
  • How to access a string value from the fragment to the main activity?
  • [OpenFileDialog-saveFileDialog error]The process cannot access the files because it is being used by
  • Behavioral to Structural Conversion Problems VHDL
  • Positioning buttons below menu bar with GridBagLayout
  • Libtool issue while compiling Liblinphone
  • Largest prime factor - euler project
  • Crc16 To String
  • App Engine OAuth2.0 authorized cron job to analyze Google Sheet
  • how to get text from in nested html elements using jericho?
  • How to load Images in Codeigniter?
  • django point definition
  • Why doesn't the GCC assembly output generate a .GLOBAL for printf
  • Google Text To Speech as a sperate class that can be called when ever needed?
  • Expand in the same as
  • Robotium & system dialogs
  • Tkinter button 'not defined' when ran
  • How to parse nested JSON with GSON
  • What should be the grok pattern for thoses logs ? (ingest pipeline for filebeat)
  • Running a Service from ASyncTask or Thread?
  • How to Set wallpaper of image with its actual size?
  • Ransack search string arrays stored in db
  • Align Excel cell to center VB - xlCenter is not declared
  • Back press fragments issue in android
  • Mapping ManyToMany with composite Primary key and Annotation:
  • MRO with multiple inheritance in python
  • How to populate html table with info from list in django
  • countdown bar android example
  • Prevent page break in text block with iText, XMLWorker
  • Shouldn't else be indented in the below code
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How to stop GridView from loading again when I press back button?
  • JaxB to read class hierarchy