0

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.

  1. Using onPrepareOptionsMenu instead of onCreateOptionsMenu in Fragment
  2. onPrepareOptionsMenu not getting called in Fragments
  3. https://android.i-visionblog.com/android-changing-menu-items-at-run-time-48970f0cf1b7

Mainly followed this link:

  1. How to change menu item text dynamically in Android

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"
    }
}
DarkFantasy
  • 240
  • 3
  • 16

1 Answers1

1

Here is my approach:

Added setHasOptionsMenu(true) to

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
    setHasOptionsMenu(true)

    ...
}

then

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"
    }
}

and it is working perfectly!!

Ronak Sethi
  • 607
  • 5
  • 12
  • Hi. Thank you for your response. It's the same thing. The code doesn't execute `onCreateOptionsMenu()`. I am posting my whole code. If it's okay please have a look. – DarkFantasy Jun 10 '20 at 17:05
  • I edited my answer and checked it also. In your code, you wrote `item.title == "Unarchive"` replace it with `item.title = "Unarchive"` . Hope it works now. – Ronak Sethi Jun 10 '20 at 17:25
  • Thank you for your response. The issue with my code is that it doesn't execute it only. I am calling this fragment from an adapter. I don't know why it doesn't execute that part. Is there any other way, you reckon? – DarkFantasy Jun 10 '20 at 17:30
  • Can you share your git repo link so that I can help you more effectively? I am also calling my fragment from the adapter! – Ronak Sethi Jun 10 '20 at 17:40