0

My app uses navigation component, I have CreditFragment and it uses TabLayout with ViewPager2, it contains CreditTabFragment, when I am at the CreditTabFragment I want to navigate to NeedHelpFragment using findNavController().

How can I make the Navigation Controller know I'm on creditTabFragment?

I tried to create a childFragmentManager but without success

ClickListener of the button that navigates to NeedHelpFragment

Sweta Jain showed this error, I couldn't add Navigation.findNavController().navigate(action) from the Answer

2 Answers2

0

You may try doing this:

import androidx.navigation.Navigation

    buttonNeedHelp.setOnClickListener {it: View!
     val action =
             CreditTabFragmentDirections.actionCreditTabFragmentToNeedHelpFragment(args) // add args in the bracket, as needed
            Navigation.findNavController(it)
                .navigate(action)
    Navigation.findNavController().navigate(action)
    }

OR, If you have no args, try using the ID of your action from the navigation graph,

buttonNeedHelp.setOnClickListener {it: View!
        Navigation.findNavController(it)
         .navigate(R.id.action_creditTabFragment_to_needHelpFragment)
}
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
  • The "Navigation.findNavController().navigate(action)" showed the error: None of the following functions can be called with the arguments supplied. – Marcelo Junior Oct 06 '21 at 15:27
  • Please try the second one, if you have no args to pass. – Sweta Jain Oct 06 '21 at 17:02
  • Hi, still with erro, the main problem is that i'm using tab layout, and the CreditFragment(Father) don't have the navigation action, the CreditTabFragment(Child) has it, so the erro that appears is that Navigation dint't found the action, because even being in the child fragment it probably uses the parent's fragment manager, we need to solve this problem making he knows that we are in the child manager. – Marcelo Junior Oct 07 '21 at 11:47
  • Try adding your fragment to the main navigation graph. – Sweta Jain Oct 07 '21 at 13:17
  • Hi, the 3 fragments(CreditFragment, CreditTabFragment, NeedHelpFragment) are in the same nav graph, the probblem is the same, the Tab Layout, as the Tabs are inside the Fragment that contains them this ends up making me always in the Parent Fragment, the children are in the Tab Layout, so any navigation (no matter what nav graph I find, as the action does not exist in the Parent ) points out as non-existent in this Father since the Actions are in the children I doubt that you can't create a navigation with a Parent Fragment and that it can't have a child with other navigations within it. – Marcelo Junior Oct 08 '21 at 15:56
0

I Used child fragment manager where i set my view pager adapter: viewPager.adapter = SectionsPageAdapter(childFragmentManager, lifecycle) With this you will be able to use navigation, note: the navigations must be in the father fragment, not in the tabs layouts.