private void selectMenuSection(int position) { Section section = menuAdapter.getItem(position); if (section != currentSection) { // Switch to new section FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); Fragment f = fm.findFragmentById(R.id.content); if (f != null) { if (currentSection.shouldKeep()) { ft.detach(f); } else { ft.remove(f); } } String fragmentClassName = section.getFragmentClassName(); if (section.shouldKeep() && ((f = fm.findFragmentByTag(fragmentClassName)) != null)) { ft.attach(f); } else { f = Fragment.instantiate(MainActivity.this, fragmentClassName); ft.add(R.id.content, f, fragmentClassName); } ft.commit(); currentSection = section; updateActionBar(); menuAdapter.notifyDataSetChanged(); } }