在我的应用程序中,我通过这些代码行提供导航抽屉
switch (menuItem.getItemId()) {
case R.id.home:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
fragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.list_view_container, fragment).commit();
return true;
case R.id.current_event:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.college_events:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.national_events:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.workshops:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.trainings:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, "Launching " + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.companies:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.talents:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.games:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.submission:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.about:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
每次新选择进行时,片段会一次又一次地重建我只想暂停每个片段的最后状态并唤醒他们的选择而不重建。
答案 0 :(得分:0)
你应该使用这样的东西:
case 1:
if(getSupportFragmentManager().findFragmentByTag("ranking")==null) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_holder,
new RankingFragment(), "ranking").
addToBackStack("ranking").
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
} else {
getSupportFragmentManager().popBackStack("ranking", 0);
}
break;