In my Fragment class, I have placed:
setHasOptionsMenu(true)
 requireActivity().invalidateOptionsMenu()
and I am overriding the onPrepareOptionsMenu() like this
override fun onPrepareOptionsMenu(menu: Menu) {
    val item : MenuItem = menu.findItem(R.id.home_popup_archive)
    requireActivity().invalidateOptionsMenu()
    if(item.title == "Archive"){
        item.title == "Unarchive"
    }
    return super.onPrepareOptionsMenu(menu)
}
I also tried to override onCreateOptionsMenu, but the function code doesn't execute. There's no error though.
These are the links I have looked at. None helped.
- Using onPrepareOptionsMenu instead of onCreateOptionsMenu in Fragment
- onPrepareOptionsMenu not getting called in Fragments
- https://android.i-visionblog.com/android-changing-menu-items-at-run-time-48970f0cf1b7
Mainly followed this link:
I am trying to change the text of a menu item. Would appreciate some help.
UPDATE Posting the whole code now for help.
class ShowAllArchivedImagesFragment : Fragment() {
private lateinit var bindingUserImages: FragmentShowAllUserImagesBinding
private var adapter: OptionsForArchivedImagesAdapter? = null
private var recyclerView: RecyclerView? = null
private var userImagesList: MutableList<HomePostModel>? = null
private var getPostId: String = ""
private var getPostPosition: String = ""
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    setHasOptionsMenu(true)
    //        bind and inflate layout
    bindingUserImages = FragmentShowAllUserImagesBinding.inflate(layoutInflater)
    //        get recyclerview id
    recyclerView = bindingUserImages.fragShowAllUserImagesRecyclerView
       //        get layout manager
    val linearLayoutManager = LinearLayoutManager(context)
    //        speed ups
    recyclerView!!.setHasFixedSize(true)
    recyclerView!!.setItemViewCacheSize(20)
    recyclerView!!.layoutManager = linearLayoutManager
    //        set adapter
    userImagesList = ArrayList()
    adapter =
        context?.let { OptionsForArchivedImagesAdapter(it, userImagesList as ArrayList<HomePostModel>) }
    recyclerView!!.adapter = adapter
    //        get postid, postPosition from ShowUploadedImagesOnProfileAdapter
    val preference = context?.getSharedPreferences("PREFS", Context.MODE_PRIVATE)
    if (preference != null) {
        getPostId = preference.getString("postId", "none").toString()
        getPostPosition = preference.getString("postPosition", "none").toString()
    }
    DisplayUserArchivedImages()
    bindingUserImages.fragmentShowAllUserImagesCloseBtn.setOnClickListener {
        requireActivity().onBackPressed()
    }
     //        requireActivity().invalidateOptionsMenu()
    // Inflate the layout for this fragment
    return bindingUserImages.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)
    inflater.inflate(R.menu.home_fragment_pop_up_menu_item,menu)
    val item: MenuItem = menu.findItem(R.id.home_popup_archive)
    if(item.title == "Archive")
    {
        item.title == "Unarchive"
    }
}
 
    