Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

android - show/hide especific items from toolbar menu not working kotlin

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?

question from:https://stackoverflow.com/questions/66056515/show-hide-especific-items-from-toolbar-menu-not-working-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...