Minoru
Minoru

Reputation: 1730

Fragment content not loading on BottomNavigationView FragmentTransaction

I have an application that uses a BottomNavigationView to navigate between Fragments. One of the Fragments (SearchFragment) has a Tab view, implemented with EasyTabs.

When I navigate to SearchFragment for the first time, the layout loads correctly, with all 3 Tabs populated. The problem is that when I navigate to another Fragment and return to SearchFragment, the layout doesn't load. I get the Tab view in the top of screen, but the content is not shown, in a way that I need to navigate between the Tabs to make them appear one by one.

onNavigationItemSelected method:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.tab_search:
            mFragment = new SearchFragment();
            break;
                /*case R.id.tab_history:
                    mFragment = new RadioFragment();
                    break;*/
        case R.id.tab_desc:
            Bundle bundle = new Bundle();
            bundle.putBoolean("fromSetup", false);
            mFragment = new DescriptionFragment();
            mFragment.setArguments(bundle);
            break;
                /*case R.id.tab_config:
                    mFragment = new RadioFragment();
                    break;*/
    }
    final FragmentTransaction transaction = mFragmentManager.beginTransaction();
    transaction.replace(R.id.main_container, mFragment).addToBackStack(null).commit();
    return true;
}

EasyTabsBuilder inside SearchFragment:

EasyTabsBuilder.with(mTabs)
            .addTabs(
                    new TabItem(new SearchByDateFragment(), ""),
                    new TabItem(new SearchByEventFragment(), ""),
                    new TabItem(new SearchByDescriptionFragment(), "")
            )
            .setTabsBackgroundColor(EasyTabsColors.White)
            .setIndicatorColor(EasyTabsColors.Gray)
            .setTextColors(EasyTabsColors.Black, EasyTabsColors.White)
            .addIcons(
                    R.drawable.ic_date_range_black_24dp,
                    R.drawable.ic_face_black_24dp,
                    R.drawable.ic_description_24px)
            .hideAllTitles(true)
            .Build();

Upvotes: 2

Views: 570

Answers (1)

Vodet
Vodet

Reputation: 1519

I got exactly the same issue with a bottom navigation and a tab layout.

The only solution I found is to remove your search Fragment and the 3 tab from the fragment manager. You can do it in the onPageSelected from your PageChangeListener

Example

        @Override
        public void onPageSelected(int position) {

           if (position != 3 && position != 2) {
                List<Fragment> fragments = getSupportFragmentManager().getFragments();
                for (int i = fragments.size() -1; i >= 0; i--) {
                    if (fragments.get(i) != null) {
                        if (fragments.get(i).getClass() == OrderInProgressFragment.class ||
                                fragments.get(i).getClass() == OrderFinishFragment.class ||
                                fragments.get(i).getClass() == OrderFragment.class) {
                            getSupportFragmentManager().beginTransaction().remove(fragments.get(i)).commit();
                            getSupportFragmentManager().executePendingTransactions();
                        }
                    }
                }
            }

            bottomNavigationView.getMenu().getItem(position).setChecked(true);
        }

I got 4 page on my bottomNavigation and my fragment to remove are in the 3 position. So the if is for check if I am using my fragment currently or if I am on the previous tab, if not I remove the OrderFragment which contains tab and the two other Tab inside it

Upvotes: 3

Related Questions