Here is my code I want to update menu item icon based on data I requesting in server that is why I need to have access to menu item in onViewCreated as I think solution which I am doing now is not right as observing data in onPrepareOptionsMenu?
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_favorite, menu)
}
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
viewModel.favorite
.observe(viewLifecycleOwner, Observer {
if (it.data != null) {
menu.findItem(R.id.action_favorite)?.icon = true
}
})
}