As for NavigationDrawerItem replace FragmentAdapter?

Hello have the following problem :
When choosing a NavigationView item I is generated PageFragmentAdapter's each with a different number of tabs but the problem is that if I already created FragmentAdapter then selecting another item a it does not overwrite the existing fragments.
For clarity here is the code
MainActivity
private void initNavigationView() {
 drawerLayout = (DrawerLayout) findViewById(R. id.drawer_layout);

 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R. string.view_navigation_open, R. string.view_navigation_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();

 NavigationView navigationView = (NavigationView) findViewById(R. id.navigation);
 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
 public boolean onNavigationItemSelected(MenuItem item) {
drawerLayout.closeDrawers();
 int id = item.getItemId();

 if (id == R. id.AccountItem) {
 toolbar.setTitle( getResources().getString(R. string.navigation_acoount));
 if (viewPager == null ){
 viewPager = (ViewPager) findViewById(R. id.viewPager);
}

 AccountPagerFragmentAdapter adapter1 = new AccountPagerFragmentAdapter(context,getSupportFragmentManager());

viewPager.setAdapter(adapter1);

 if (tabLayout != null){
 tabLayout = (TabLayout) findViewById(R. id.tabLayout);
}
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

 } else if (id == R. id.NavigationServices) {
 toolbar.setTitle( getResources().getString(R. string.navigation_services));
 if (viewPager == null ){
 viewPager = (ViewPager) findViewById(R. id.viewPager);
}

 ServicesPagerFragmentAdapter adapter2 = new ServicesPagerFragmentAdapter(context,getSupportFragmentManager());
viewPager.setAdapter(adapter2);

 if (tabLayout != null){
 tabLayout = (TabLayout) findViewById(R. id.tabLayout);
}
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

 } else if (id == R. id.NavigationCatalog) {
 toolbar.setTitle( getResources().getString(R. string.navigation_catalog));
 if (viewPager == null ){
 viewPager = (ViewPager) findViewById(R. id.viewPager);
}

 CatalogPagerFragmentAdapter adapter3 = new CatalogPagerFragmentAdapter(context,getSupportFragmentManager());

viewPager.setAdapter(adapter3);

 if (tabLayout != null){
 tabLayout = (TabLayout) findViewById(R. id.tabLayout);
}
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

 } else if (id == R. id.NavigationSettigs) {
 Intent intent = new Intent(MainActivity.this SettingActivity.class);
startActivity(intent);
 } else if (id == R. id.NavigationAbout) {
 Intent intent = new Intent(MainActivity.this AboutActivity.class);
startActivity(intent);
}

 DrawerLayout drawer = (DrawerLayout) findViewById(R. id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
 return true;
}
}
);
 }

+ Code of one of the fragments
CatalogPagerFragmentAdapter
package com.studio.autocomplex.client.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.studio.autocomplex.client.fragment.AbstractTabFragment;
import com.studio.autocomplex.client.fragment.NewCarsFragment;
import com.studio.autocomplex.client.fragment.TestDriveFragment;
import com.studio.autocomplex.client.fragment.UsedCarsFragment;

import java.util.HashMap;
import java.util.Map;

public class CatalogPagerFragmentAdapter extends FragmentPagerAdapter{

 private Map<integer, abstracttabfragment> tabs;
 private Context Context;


 public CatalogPagerFragmentAdapter(Context Context, FragmentManager fm) {
super(fm);

 this.context = context;

 tabs = new HashMap<>();
 tabs.put(0, NewCarsFragment.getInstance(context));
 tabs.put(1, UsedCarsFragment.getInstance(context));
 tabs.put(2, TestDriveFragment.getInstance(context));
this.notifyDataSetChanged();
}

@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();
}
}</integer>


What did I do wrong ?? And please do not kick for the code I later carefactor
July 9th 19 at 13:27

Find more questions by tags Android