There is a NavigationView with items from the fragments, one of the fragments contains a ViewPager and TabLayout with three fragments. If you select another menu item, the current fragment is replaced, and the tabs in the tab remain hanging active. Therefore, when returning to this fragment, the empty fragments hang. What can help me in this situation?

MainActivity

public class MainActivity extends AppCompatActivity { private static final int LAYOUT =R.layout.main_layout; private DrawerLayout drawerLayout; private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.AppDefault); super.onCreate(savedInstanceState); setContentView(LAYOUT); initNavigationView(); initToolbar(); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.view_navigation_open,R.string.view_navigation_close); drawerLayout.setDrawerListener(toggle); toggle.syncState(); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new MainFragment(),"TAG") .commit(); } } private void initNavigationView() { drawerLayout = (DrawerLayout)findViewById(R.id.draw); NavigationView view = (NavigationView)findViewById(R.id.navigation); view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { switch (menuItem.getItemId()){ case R.id.actionWhereEat: MainFragment mainFragment = new MainFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.container,mainFragment).addToBackStack(null).commit(); break; case R.id.actionMyOrders: BasketFragment basketFragment = new BasketFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.container,basketFragment).addToBackStack(null).commit(); break; case R.id.actionPromotions: break; case R.id.actionPersonalRoom: break; } drawerLayout.closeDrawers(); return true; } }); } private void initToolbar() { toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setLogo(R.drawable.logo); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return false; } }); toolbar.inflateMenu(R.menu.menu); }} 

MainFragment

 public class MainFragment extends Fragment { private ViewPager pager; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mainFragment = inflater.inflate(R.layout.activity_main_fragment,container,false); initTabs(mainFragment); return mainFragment; } private void initTabs(View v) { pager = (ViewPager)v.findViewById(R.id.viewPager); TabsActionAdapter adapter = new TabsActionAdapter(v.getContext(), getFragmentManager()); pager.setAdapter(adapter); pager.setOffscreenPageLimit(3); TabLayout tabLayout = (TabLayout)v.findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(pager); }} 

TabActionAdapter

 public class TabsActionAdapter extends FragmentPagerAdapter { private Map<Integer, AbstractFragment> tabs; private Context context; public TabsActionAdapter(Context context, FragmentManager fm) { super(fm); this.context = context; initTabsMaps(context); } @Override public CharSequence getPageTitle(int position) { return tabs.get(position).getTitle(); } @Override public Fragment getItem(int position) { return tabs.get(position); } @Override public int getCount() { return tabs.size(); } private void initTabsMaps(Context context) { tabs = new HashMap<>(); tabs.put(0, MapFragment.getInstance(context)); tabs.put(1, MenuFragment.getInstance(context)); tabs.put(2, PaymentFragment.getInstance(context)); }} 
  • Nested fragments = problems. Redo this fragment with fragments in the activation. - Yuriy SPb
  • Thanks for the good advice! - mtripilec 2:51 pm
  • If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Nicolas Chabanovsky

0