Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPrepareOptionsMenu(Menu) is not being invoked after pressing the back button

Tags:

android

I am trying to invoked the onPrepareOptionsMenu() in the MainActivity after pressing the back button in the Map activity but it is not being invoked after it was created once. According to the documentation it must be called right before the menu is shown. Is this case valid after invoking the onBackPressed()?

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

like image 644
MrPencil Avatar asked Mar 15 '23 21:03

MrPencil


2 Answers

Yea you can just add this in your activity. This will make it be called anytime you come back from any activity though.

@Override
public void OnStart()
{
   super.onStart();
    invalidateOptionsMenu();
}
like image 117
Tomer Shemesh Avatar answered Apr 30 '23 14:04

Tomer Shemesh


This is how create and prepare works for menu

onCreateOptionsMenu() called only once when the Activity or Fragment is created

onPrepareOptionsMenu() usually used for some dynamic control contents

In order to onPrepareOptionsMenu() to be triggered you have to call invalidateOptionsMenu()

like image 45
Derek Fung Avatar answered Apr 30 '23 15:04

Derek Fung