In my android application, I had one main activity(ieHome) and two full-screen fragments. I can access theses three using Navigation Drawer. These two fragments act as the separate screen but actually its top on main activity. In my case, I clicked the first fragment then I clicked the second fragment. During this time, I closed the first fragment and displayed the second fragment. But, main activity screen was now displayed one second during fragment transition.
@SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out); if(!item.isChecked()) getSupportFragmentManager().popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE); item.setChecked(true); if (previousMenuItem != item.getItemId()) { if (item.getItemId() == R.id.fragment_two) { initialiseFragmentTwo(); } else if (item.getItemId() == R.id.nav_main_activity) { getSupportFragmentManager(). popBackStackImmediate(null, POP_BACK_STACK_INCLUSIVE); getSupportActionBar().setTitle(R.string.app_name); } else if(item.getItemId() == R.id.nav_fragment_one){ initialiseFragmentOne(); } } previousMenuItem = item.getItemId(); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer != null) { drawer.closeDrawer(GravityCompat.START); } return true; }
Instead of using POP_BACK_STACK_INCLUSIVE try to use replace
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_gallery) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_slideshow) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_manage) {
fragmentClass = FragmentTwo.class;
} else if (id == R.id.nav_share) {
fragmentClass = FragmentOne.class;
} else if (id == R.id.nav_send) {
fragmentClass = FragmentTwo.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
See here below links
http://chrisrisner.com/Using-Fragments-with-the-Navigation-Drawer-Activity
https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.