0

Iam creating a menu toolbar in MainActivity, that have body fragment (the only toolbar is set in the activity not within the fragments)

When the default fragment is set (HomeFragment), I want to hide search and sort icons from the toolbar menu, else to show them in the other ContentFragment

ContentFragment can take different toolbar names verbs, nouns, pronouns.. and HomeFragment only Home

this is why Im using if(supportActionBar!!.title== "Home") {

MainActivity.kt

  class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
   transaction(HomeFragment)...commit()
    .....
  }

 //below bundle ends
  override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
    if(supportActionBar!!.title== "Home") {
        menu?.findItem(R.id.db_menu_search)?.isVisible  = false
        menu?.findItem(R.id.db_menu_sort)?.isVisible   = false
    }else{
        menu?.findItem(R.id.db_menu_search)?.isVisible  = true
        menu?.findItem(R.id.db_menu_sort)?.isVisible   = true
    }

    return super.onPrepareOptionsMenu(menu)
}


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.header_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when (item.itemId) {
            R.id.db_menu_search -> {

                Toast.makeText(applicationContext, "Search Clicked", Toast.LENGTH_SHORT)
                    .show()
            }
            R.id.db_menu_sort -> {
                sortDialog()
            }

            R.id.header_menu_verbs -> {
                getToFragmentB("Verbs",1)
            }
          
        }
        return false
    }

header_menu.xml

 //just informative

 <item id=db_menu_search icon=ic_search />
 <item id=db_menu_sort icon=ic_sort />
 <item id=db_menu_verbs />
   <item id=db_menu_nouns /> ....

I added onPrepareOptionsMenu method expecting to make it work...it doesnt work properly

what am doing wrong?

How can I hide those two icons from the toolbar when HomeFragment is set in the activity and show them once the another fragment is displayed?

morty
  • 17
  • 5
  • I think, that the problem is that you are detecting current fragment by it title. I don't know how do you set the title for each fragment, so it could be wrong title, when onPrepareOptionsMenu get called. To detect current fragment you should use fragmentManager: supportFragmentManager.findFragmentById(R.id.fragment_id) then you can use it in onPrepareOptionsMenu fragment != null && fragment.isVisible { show / hide icons depending on fragment }. How to find fragment here https://stackoverflow.com/questions/9294603/how-do-i-get-the-currently-displayed-fragment – Artem Chernousov Feb 05 '21 at 04:54
  • thank you. but I dont actually understand your point. so you tell me to check what fragment is being used at that especific moment, and use `onPrepare...` whitin the fragment code? or within the Main activity class. where should I supossed to use the code to make it work – morty Feb 05 '21 at 14:35
  • I wrote, that I'm thinking, that in the moment of call onPrepareOptionsMenu you have the wrong title in toolbar, and toggle icons working wrong as the result. If you want, I can write working solution for you later. – Artem Chernousov Feb 05 '21 at 14:43

1 Answers1

1

I describe main steps here, link to the working solution in the end

  1. All items in menu.xml have parameter android:visible="false"

  2. setHasOptionsMenu(true) should be in onCreate() of each fragment, where render icons in toolbar is necessary. Without it activity functions

    onCreateOptionsMenu / onPrepareOptionsMenu will never get called when fragment changes.

     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
    
         setHasOptionsMenu(true)
     }
    
  3. Handling showing/hiding icons in onPrepareOptionsMenu via supportFragmentManager. It's only the way to detect what fragment currently is displaying.

       override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
     supportFragmentManager.fragments.forEach {
       when {
         it.tag == FragmentTag.Fragment1.tag && it.isVisible -> {
           menu?.findItem(R.id.action_fragment1)?.isVisible = true
           menu?.findItem(R.id.action_fragment2)?.isVisible = true
         }
         it.tag == FragmentTag.Fragment2.tag && it.isVisible -> menu?.findItem(R.id.action_fragment2)?.isVisible = true
         it.tag == FragmentTag.Fragment3.tag && it.isVisible -> menu?.findItem(R.id.action_fragment3)?.isVisible = true
       }
     }
    
     return super.onPrepareOptionsMenu(menu)
    

    }

Code to working solution: https://github.com/yellow-cap/android-handle-toolbar-icons

  • My friend, I tested that and works like a charm, not only solved this issue, I did lern something new as well!! +1 – morty Feb 06 '21 at 15:24
  • I have this other question..do you know how to solve it? thank you https://stackoverflow.com/q/66122287/13741397 – morty Feb 09 '21 at 20:30