It is taking a lot of time in switching the fragments.
I am using this code ->
navigationView.setNavigationItemSelectedListener(
item -> {
item.setChecked(true);
drawerLayout.closeDrawers();
switch (item.getItemId()){
case R.id.places :
PlacesFragment fragment1 = new PlacesFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_content, fragment1);
transaction.addToBackStack(null);
transaction.commit();
break;
case R.id.restaurants :
RestaurantsFragment fragment2 = new RestaurantsFragment();
FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
transaction1.replace(R.id.main_content, fragment2);
transaction1.addToBackStack(null);
transaction1.commit();
break;
case R.id.about :
AboutFragment fragment3 = new AboutFragment();
FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.main_content, fragment3);
transaction2.addToBackStack(null);
transaction2.commit();
break;
case R.id.hotels :
HotelsFragment fragment4 = new HotelsFragment();
FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
transaction3.replace(R.id.main_content, fragment4);
transaction3.addToBackStack(null);
transaction3.commit();
}
return true;
}
);
Is there any faster way to do that ?
It looks like you posted long piece of code, consider editing it out and putting it on hastebin.com and pasting link to it instead. Alternatively, send your code in a file.
How much time does it take?
Is there need to define 3 different transactions?? You could define only one transaction outside switch... And call it inside different cases
Обсуждают сегодня