Search code examples
androidfragmentandroid-tablayout

Send data between fragments


I have used this tutorial to learn how to pass data between fragments:

https://www.journaldev.com/14207/android-passing-data-between-fragments

And it works perfectly if I have two fragments. But my application needs more (7) fragments. The fragments are tabs on a tablayout. When I switch back and forth between two tabs next to eachother it's good. But as soon as I switch from tab0 to a tab further away (tab5), send message, then check it on tab0, the EditText gets back it's original value.

When I change the value of an EditText, I change the value of the tab's title. And it updates properly. (I go from tab0 to tab5, send the message, tab0's title changes, so the message is received.) But when I go back to tab0, it changes back it's original value (and also it's tab title, but that is correct working).

It's like the desired changes happen, but when I switch back, it gets back the original value. Does it have something to do with destroying and savedInstances? How to get rid of this?

In STab.java (code of a tab):

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        SD = (SendData) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException("Error in retrieving data. Please try again");
    }
}
interface SendData {
    void setStructureBonusTileSpinner(int position, int currentTabNumber);
}

protected void setStructureBonusTileSpinner(int position){
    spinnerStructureBonusTile.setSelection(position);
}

Also in STab.java, in the onViewCreated method:

spinnerStructureBonusTile.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            SD.setStructureBonusTileSpinner(spinnerStructureBonusTile.getSelectedItemPosition(), tabNumber);
        }
    });

In MainActivity.java which implements STab.SendData:

@Override
public void setStructureBonusTileSpinner(int position, int currentTabNumber) {

    for(int i = 0; i < mSectionsPagerAdapter.getCount(); i++){

        if(i != currentTabNumber) {
            String tag = "android:switcher:" + R.id.container + ":" + i;
            STab f = (STab) getSupportFragmentManager().findFragmentByTag(tag);
            if(f != null){
                f.setStructureBonusTileSpinner(position);
            }
        }

    }

}

Solution

  • The off screen page limit is set to 1 by default, so those fragments beyond the two next to each other that you say that work, are being recreated from the adapter.

    If you are not going to perform heavy tasks, it would be enough just changing that limit.

    mViewPager.setOffScreenPageLimit(fragmentCount - 1)
    

    PD: Just as a tip, you should never have more than 3 tabs, it would be better if you could think on a better aproach rather than this.

    PD2: You should also post relevant code