简体   繁体   中英

navigation drawer nested fragments

I've tried almost everything but nothing seems to work. I have a navigation drawer with these fragments, say: A - viewpager B - listview C - listview Now in B and C, listview are clickable items with each having their own fragments. So ListViewA has a fragment, ListViewB has a fragment. I want proper back navigation. This is how I'm doing it, when initializing the navigation drawer:

Fragment fragment = getHomeFragment();
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                        android.R.anim.fade_out);
                fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
                fragmentTransaction.commit();

And when choosing an option from listview:

final FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(android.R.anim.fade_in,
                        android.R.anim.fade_out);
                ft.replace(R.id.frame, new FragmentB(),"HubSettings");
                ft.addToBackStack(null);
                ft.commit();

If I put addtoBackStack() in both transactions, back navigation works fine but navigation drawer title is not being set properly. I want to disable back button and force changing fragments from navigation drawer so that action bar title is set properly. How do I disable back button when it comes to CFragment?

One can go from menu like: Navigation Drawer->B Fragment->Listview option 1 Fragment Pressing Back -> Back to B Fragment -> Disable Back Button

Just override onBackPressed in your Activity which contains the fragments like this:

@Override
public void onBackPressed() {

    int count = getSupportFragmentManager().getBackStackEntryCount();       

    if (count > 0) {
        getSupportFragmentManager().popBackStack();
        updateDrawerToggle();            
    }

}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM