Search code examples
androidkotlinfragmenttoolbar

Whenever i try to hide a toolbar within fragment, app changes toolbar title first, and only after that hides itself


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.


Solution

  • 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.