简体   繁体   中英

How to open a fragment from another fragment using the Android Navigation Drawer?

I have anAndroid navigation drawer with a menu that tapping on items open fragments. Good so far. Some of those fragments need to open a new one for more details. I do that using this code from the first fragment:

getParentFragmentManager().beginTransaction().replace(R.id.nav_host_fragment_content_main, new MySecondFragment(), null).commit();

Tappin on the backbutton (the backarrow on the top-left corner) doesn't take me back to MyFirstFragment. It throughs an exceptions:

java.lang.IllegalStateException: Fragment MyFirstFragment not associated with a fragment manager.

I can use this other code:

getParentFragmentManager().beginTransaction().add(R.id.nav_host_fragment_content_main, new MySecondFragment(), null).commit();

But with this code both fragments are visible at the same time. I mean, MySecondFragment views are displayed but I see in the background MyFirstFragment views.

Any idea on how I can open MySecondFragment from MyFirstFragment, and then, if the back arrow is pressed I want to return to the same place MyFirstFragment was before.

Thanks

But with this code both fragments are visible at the same time.

You can set background color to its root layout.xml

Also you can use fragmentTransaction.addToBackStack(null) to add this transaction to the back stack

Filip Petrovski solution In Java code:

In the AppCompatActivity with the Navigation Drawer:

    @Override
    public boolean onSupportNavigateUp() {

        FragmentManager oChildFragmentManager = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main).getChildFragmentManager();
        if(oChildFragmentManager.getBackStackEntryCount() > 1){
            oChildFragmentManager.popBackStack();
            return true;
        }

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
    }


    @Override
    public void onBackPressed() {

        FragmentManager oChildFragmentManager = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main).getChildFragmentManager();
        if(oChildFragmentManager.getBackStackEntryCount() > 1){
            oChildFragmentManager.popBackStack();
            return;
        }

        super.onBackPressed();
    }

Any time you open a fragment do not forget addToBackStack :

getParentFragmentManager().beginTransaction().replace(R.id.nav_host_fragment_content_main, new MyNewFragment()).addToBackStack("").commit();

Adding your fragment to the backstack should solve your issue

FragmentTransaction ft = getParentFragmentManager().beginTransaction()

ft.replace(R.id.nav_host_fragment_content_main, new MySecondFragment(), null)
ft.addToBackStack(MySecondFragment().class.getName()) // you can use a string here, using the class name is just convenient 
ft.commit();

When pressing the back button, you will navigate through the back stack.

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