0

I am creating an app with 4 fragments (Home, Webshop, Cart, Profile). I have one activity (MainActivity) and a NavigationBar. When I'm on the Home page, I don't use the NavigationBar so it's hidden, I use the icons on the page instead for navigation (with Navigation Graph actions) and for this I use the icons' onClickListeners. So if I click on for example the Webshop, it nicely takes me to the Webshop fragment, from here I can navigate to the Cart and the Profile fragments. But not to the Home one... Whenever I click on the Home icon on the NavigationBar, it takes me to the Webshop (the fragment I chose the first time I navigated from the Home page). Same thing happens if I choose any other fragments the first time: if I choose the Cart first, then form then on whenever I click on the Home icon, it takes me to the Cart, and so on. I have no idea what could be the problem. It seems like its permanently setting that if I navigate to the Home page, the setOnClickListener - that I used for the first time - is firing.

Here I first choose the Webshop, then it always takes me to the Webshop: enter image description here

Here I first choose the Cart, then it always takes me to the Cart: enter image description here

Here I first choose the Profile, then it always takes me to the Profile: enter image description here

I have a feeling I can't use NavigationGraph and NavigationBar together, I tried to merge them together somehow but could not figure it out.

Here's some code:

HomePageFragment enter image description here

WebshopFragment enter image description here

CartFragment enter image description here

ProfileFragment enter image description here

MainActivity enter image description here

Bottom Navigation Menu XML enter image description here

NavigationGraph enter image description here

1 Answers1

0

On your MainActivity create global navController on top like you did for appBarConfiguration:

private lateinit var navController: NavController

Change val navController = findNavController(R.id.nav_host_fragment_activity_main) with navController = findNavController(R.id.nav_host_fragment_activity_main)

Add below line like so:

        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

And correct your function with this:

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

I could not write your code all over again because you've pasted in picture, if that does not work let me know.

Mert
  • 904
  • 4
  • 9
  • 21
  • Thank you, but unfortunately that did not work, maybe the problem lies somewhere else, but I am trying to figure it out since then, I tried to simplify my question, could you please look at this one here too? https://stackoverflow.com/questions/74647961/how-to-set-in-android-to-be-navigated-to-the-same-fragment-every-time-from-the-b It's the same problem but I just tried to ask it in a more straightforward way here, because I feel like i overcomplicated this one. – Antal Georgina Dec 01 '22 at 22:26