Modify the ImageView style in menu

Ineffective methods:

Add to onCreate of FragmentB

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }
Copy the code

Called where you need to refresh

requireActivity().invalidateOptionsMenu()
Copy the code

In theory it’s going to be executed

override fun onPrepareOptionsMenu(menu: Menu) {
        super.onPrepareOptionsMenu(menu)
    }
Copy the code

End result: onPrepareOptionsMenu() is not executed

Final test result: onCreateOptionsMenu() and onPrepareOptionsMenu() were not executed from start to finish in FragmentB.

The current solution without damaging the structure is as follows:

private fun refreshMenu(isHas:Int) { toolbar.menu.clear() toolbar? .run { if (1 == isHas){ inflateMenu(R.menu.menu1) }else{ inflateMenu(R.menu.menu2) } setOnMenuItemClickListener { when (it.itemId) { } true } } }Copy the code

Reload different menu files via isHas. Note that you must call tooling.menu.clear () before reloading, otherwise you will get an unexpected interface.

There is a better way, please leave a comment!