Search code examples
androidnavigation-drawerandroidxandroid-jetpackandroid-jetpack-navigation

I want to redirect to a fragment from one fragment onbuttonclick (jetpack naviagtion)


Im using a jetpack navigation in new androidx. In from home i have small 3 button to easily navigate to fragments that i put in navigation drawer. Since I'm not experianced in this its very confusing to me. I have home, projects and payments.. i have to navigate from home to project and payment tab.. anyone know anyways please help me..

I GOOGLED MANY TIMES WHOLE DAY BUT NOTHING WERE HELPFUL TO ME. i tried but failed the below code.

FragmentTransaction fragmentTransaction = getActivity()
                            .getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main, new ProjectFragment());
                    fragmentTransaction.commit();

this is the naviagtion bar img

and here is home one..

img

since im new to this ..any help will be great.. thank you.


Solution

  • Android Navigation Components handle fragments transaction and back stack automatically and that's the whole point behind this component.

    In your fragment:

    findNavController().navigate(id)
    

    This id could be either action id or destination id.

    I recommend navigating by action id.

    Actions are arrows connecting two destinations together. Destination is another word for a fragment.


    If you need to access your navigation inside Activity:

    findNavController(containerId).navigate(id)
    

    constainerId = the id of FragmentContainerView inside your XML.