2

How can i send data from Activity to tab fragment when FloatingActionButton is clicked OR where do get it wrong and how can i fix it,I know it has been asked number of times and i tries all solution include this this and this which gave clear explanation but still work for more that 20hr of working i got no error and nothing works.

//float button in main activity

         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
   fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String  MyValue=" Hi fragment";
            Bundle bundle = new Bundle();
            bundle.putString("value", MyValue);
          // set Fragmentclass Arguments
            TabFragment1 fragobj = new TabFragment1();
            fragobj.setArguments(bundle);

        }
        });

//In fragment

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =inflater.inflate(R.layout.tab_fragment_1, container, false);
     TextView textView= (TextView)view.findViewById(R.id.textView) ;
    if (getArguments() != null) {
        String MyValue = getArguments().getString("value");
        textView.setText(MyValue);
    }
    return view;
}

//// here is the 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;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    final Fragment fragment = (Fragment) super.instantiateItem(container, position);
    instantiatedFragments.put(position, new WeakReference<>(fragment));
    return fragment;
}

@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
    instantiatedFragments.remove(position);
    super.destroyItem(container, position, object);
}

@Nullable
public Fragment getFragment(final int position) {
    final WeakReference<Fragment> wr = instantiatedFragments.get(position);
    if (wr != null) {
        return wr.get();
    } else {
        return null;
    }
}

@Override
public CharSequence getPageTitle(int position) {
    return mTabHeader.get(position);
}

}

Community
  • 1
  • 1
f2k
  • 99
  • 11
  • Please post the adapter code inside your activity. where you instantiate and set fragments to it. When the fab button is pressed, you are not using your adapter to get the fragment, you are creating a new one but doing nothing with that fragment. – Jonathan Aste May 19 '17 at 17:58
  • Also add the code where you change the fragments – Jonathan Aste May 19 '17 at 18:00

1 Answers1

1

First, i assume that you want to update the fragment whenever you press the fab, even if your fragment is already on screen. your adapter is returning a new fragment each time, you should instead hold the fragments instance and return the fragment for the given position stored in your array. Check this generic FragmentStatePagerAdapter:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mFragmentTitleList = new ArrayList<>();


    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    public void addFragment(Fragment fragment) {
        mFragmentList.add(fragment);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (!mFragmentTitleList.isEmpty()) {
            return mFragmentTitleList.get(position);
        }
        return "";
    }

}

Then on your fragment create a method to update your info:

public void updateData(String string){
    miTextView.setText(string);
}

And finally on your activity do something like this:

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        YourFragment fragment = (YourFragment) adapter.getItem(desiredPosition);
        fragment.updateData("your string");
        }
    });
Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20
  • I updated my answer, let me know if it helps you, or if I missunderstood what you need – Jonathan Aste May 19 '17 at 15:55
  • what should i do with this FragmentStatePagerAdapter? or i have to replace my adapter with it?@Jonathan Aste – f2k May 19 '17 at 16:09
  • Yes replace your adapter with the one I posted – Jonathan Aste May 19 '17 at 16:10
  • i tried that it did not work, also i didnt mention before i have three fragment which uses the adapter codes i posted so when i change it it ruin the entire project with red, am kind new to android how can i make it @Jonathan Aste – f2k May 19 '17 at 16:36
  • is there a way maybe using bundle or any alternative because for this case i have to change almost every OR link where i can get a complete tutorial that express implementation of what you gave @Jonathan Aste because i real do need this.. – f2k May 19 '17 at 16:54