Java – Android – fragment does not directly update tabs that are adjacent to each other

Android – fragment does not directly update tabs that are adjacent to each other… here is a solution to the problem.

Android – fragment does not directly update tabs that are adjacent to each other

I have a FragmentActivity that contains four fragments displayed in a tab layout. These tabs are used to add or remove content from My Database and are displayed in the following order: Artist – Genre – Album – Song. The album tab contains spinner for all artists and genres in the database, which should be updated as artists or genres are added to their respective tabs. This applies to the Artist tab; However, when I add or remove something from the Genres tab, the changes don’t show up in Albums unless I change the tab to Artist, or go to a separate activity and go back.

The problem I don’t understand is that when I change the tab layout to Artist-Genre-Song-Album, everything works fine. It seems that tabs are only updated when viewing individual activities or fragments, and then return to the tab where the change should have occurred. I also know that the sql or method I use to refresh the spinner is not a problem, as I also try to change the layout to genre – artist – album – song. In this case, genre updates successfully reflect in the album, but those in the artist don’t, until I move the two tabs away from the album.

It’s hard to explain more clearly than that, I can post my code if needed, but the problem seems to be related to navigating between tabs next to each other.

I

also try to inflate in my genre fragment to add album layouts and then update the spinner as I add or remove genres.

This is AddGenreFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.add_genre_layout, container, false);
    addAlbum = inflater.inflate(R.layout.add_album_layout, container, false);
    spinGenre = (Spinner) addAlbum.findViewById(R.id.spinGenre);
    return v;
}

Then in the delete method:

// Refresh the spinners in Add Album
ArrayList<String> genreList = dbh.getAllGenres();        Return all the genres in the database
if (!genreList.isEmpty()) {
     Set the genre drop down box to be populated by all genres in the database
    adapterGenre = new ArrayAdapter<String>(addAlbum.getContext(),
    android. R.layout.simple_spinner_dropdown_item, genreList);
    adapterGenre.setDropDownViewResource(android. R.layout.simple_spinner_dropdown_item);
    spinGenre.setAdapter(adapterGenre);
}

Edit:

After stumbling upon another issue, I finally found a solution to this problem. The reason it doesn’t update and work properly is because I’m using ViewPager with fragments for my tab adapter. ViewPager’s default setting is to load up to 3 pages or fragments at a time. Therefore, if the user moves 3 tags away from a tag, the original label is destroyed and onCreate is called again when the original label is reselected.

To fix this, I just added this line to the constructor of my TabsAdapter class:

mViewPager.setOffscreenPageLimit(NUMBER_OF_TABS - 1);

This eventually solved the problem I was having, as well as a few other issues that had already arisen.

Solution

To solve this problem, you must understand how ViewPager Effective.

When you set up an adapter in ViewPager, for example 4 fragments. Let’s call | a |B |C | D |


Navigate from Activity X to Activity Y, which contains the ViewPager containing the fragment described above.

X --> Y
      |--> | A | B | C | D |

When Y’s View is rendered, fragment A is displayed

          Y
        | A | B |

Fragment B is also generated in the pipeline.


When you move to fragment B, C is generated in the pipeline and A is still there.

          Y
    | A | B | C |

Any changes you make in A that reflect in B are not rendered.


When you move to fragment C, D is generated in the pipeline and B still exists. A was killed

          Y
    | B | C | D |


When you move to D, C is retained and B is killed.

          Y
    | C | D |


When you return to C now, B generates new padding data because it was created for the first time.

          Y
    | B | C | D |

The

changes that must be reflected when changing the data in step A are now available and can be seen in the next step


When you go back to B now, A generates new fill data because it was created for the first time.

          Y
    | A | B | C |


ViewPager caches adjacent views on both sides, and any changes that affect the View are adjacent screens that will not render unless the View is forced to recreate/refresh by moving to another activity or separating the 2 Views .

Compare A, B, C, D to your tab, and to resolve this issue, look at Update ViewPager dynamically?

  • Make sure to use the FragmentStatePagerAdapter instead of the FragmentPagerAdapter, as it does not kill the fragment.

Related Problems and Solutions