0

I am trying to navigate to another fragment by clicking a button:

findNavController().navigate(FirstFragmentDirections.navigateToSecondFragment(argument))

And i am trying to hide toolbar title in second fragment:

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        (activity as AppCompatActivity).supportActionBar?.hide()
        _binding = FragmentBinding.inflate(inflater, container, false)
        return binding.root
    }

The issue is, whenever i click a button, toolbar changes its title name first (to second fragment name), and only after that navigates to second fragment and hides itself. It`s a metter of one second or even less. How can i fix this? I am using navigation drawer.

LAO
  • 87
  • 10

1 Answers1

2

Use a toolbar on activity level instead of fragment. You can add that toolbar component to your parent activity xml layout this way you would have control before navigating to another fragment.

georkost
  • 588
  • 6
  • 12
  • ty for your response! My toolbar is already declared in main activity xml file. As far as i understood, you suggest to control toolbar visibility programmaticaly via main activity kt file, am i right? I was surfing internet on this topic, found a few solutions like `if (supportFragmentManager.fragments.first()?.childFragmentManager?.fragments?.get(0)==SecondFragment()) { supportActionBar?.hide() }` – LAO Jun 02 '21 at 11:35
  • or `if (supportFragmentManager.findFragmentById(R.id.nav_host_fragment) is SecondFragment) { supportActionBar?.hide() }` But none of them worked. – LAO Jun 02 '21 at 11:39
  • This solution kinda helped - https://stackoverflow.com/a/58027509/13916204 But now there is another issue. Toolbar disappears first, and only after that navigates to another fragment. I will continue to dig into that, any suggestions will be appreciated. I want to make transition as smooth as possible. – LAO Jun 02 '21 at 12:09
  • 1
    The blog you posted is giving you a way to hide bottom bar plus the toolbar when navigating on a screen that should be full screen. Generally having a toolbar in every fragment is more consisten UI but its up to you. You got the idea, yes I was proposing to control visibility from MainActivity.kt. Making the transition look very smoothly is kinda tricky. Consider adding the hiding process inside an animation block to make it look kinda fading away – georkost Jun 02 '21 at 17:37