2

I have an app bar defined from my fragment rather than activity by using

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

        binding.toolbar.apply {
            //add menu
            inflateMenu(R.menu.menu_fragment)

            //setup with navcontroller/navgraph
            setupWithNavController(findNavController())
        }
}

The problem I'm facing is trying to implement a warning message when a user clicks the Navigate Up button using the app bar. I want this behaviour only in one fragment.

I've found solutions online pertaining to app bars defined in an activity but they don't seem to work for me (such as using override fun onSupportNavigateUp(). Any ideas if I may be able to accomplish this?

Update

Initially, I implemented the chosen answer which worked but was causing some memory leaks. The kind individual who answered this question also found a workaround for the memory leaks here . Unfortunately, it didn't work so great for me (I believe because I am using navigation components) but it may work for you.

I later realized that I could easily override the navigate up default behaviour by adding this piece of line to my toolbar code:

        binding.toolbar.apply {
            //add menu
            inflateMenu(R.menu.menu_fragment)

            //setup with navcontroller/navgraph
            setupWithNavController(findNavController())
            
            //****************ADD THIS******************
            setNavigationOnClickListener { view ->
                //do what you want after user clicks navigate up button
            }
        }
Don Robin
  • 121
  • 2
  • 16

2 Answers2

1

The problem I'm facing is trying to implement a warning message when a user clicks the Navigate Up button using the app bar. I want this behaviour only in one fragment.

So, you just need to catch the event of hitting the UP button of the app bar for that particular fragment.

You can enable the options menu for that fragment:

setHasOptionsMenu(true)

And override onOptionsItemSelected to catch the UP button id:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        // Handle the UP button here
        Toast.makeText(requireContext(), "UP button clicked", Toast.LENGTH_SHORT).show()
        return true
    }
    return super.onOptionsItemSelected(item)
}

Note: if you want to use a unique toolbar for that fragment other than the default one, check this answer.

now I am unable to inflate my menu using inflateMenu(R.menu.menu_fragment). Any ideas?

You can remove this inflation, and instead override onCreateOptionsMenu for that:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.menu_fragment, menu)
}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thank you. Yes I am using a unique toolbar for each fragment. I managed to get this to work by removing SetupWithNavController(findNavController()) from my onViewCreated and implementing the solution you suggested for unique toolbar by creating a function in my activity. However, now I am unable to inflate my menu using inflateMenu(R.menu.menu_fragment). Any ideas? – Don Robin Sep 11 '21 at 14:52
  • 1
    Nevermind, I got the menu to be inflated using override fun onCreateOptionsMenu – Don Robin Sep 11 '21 at 14:58
  • @DonRobin Good job! you can remove this inflation, and instead override `onCreateOptionsMenu` for that .. Please have a look at the updated answer – Zain Sep 11 '21 at 14:58
0

onCreateOptionsMenu() didn't work for me,

I have write this code part in onCreate() for Activity; (navigationView is id my NavigationView)

for (i in 0 until navigationView.menu!!.size()) {
        val item = navigationView.menu.getItem(i)
        val s = SpannableString(item.title)
        s.setSpan(AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0, s.length, 0)
        item.title = s
    }
Umut Can
  • 41
  • 2