1

I am inflating a menu item dynamically after specific event occurs. But menu item is not getting visible.

Below is code:

LocalBroadcastManager.getInstance(context).registerReceiver(new BroadcastReceiver() { @Override public void onReceive (Context context, Intent intent){ appCompatActivity.supportInvalidateOptionsMenu(); setHasOptionsMenu(true); setUpdateText(); appCompatActivity.invalidateOptionsMenu(); } }, new IntentFilter(UPDATE_ACTION)); void setUpdateText() { if (mainMenu != null) { mainMenu.findItem(R.id.notificationdevicemenu).setVisible(true); mainMenu.findItem(R.id.notificationdevicemenu).setEnabled(true); } } 

And I'm inflating menu like below:

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); mainMenu=menu; menu.clear(); try { inflater.inflate(R.menu.main_list_fragment, mainMenu); } catch (Exception e) { e.printStackTrace(); } } 

Can anyone please tell me why menu is not getting visible. Any kind of help will be appreciated.

1 Answer 1

0

Invalidating options menu will fire onPrepareOptionsMenu(), not onCreateOptionsMenu(), which is called once (the first time options menu is displayed). Override onPrepareOptionsMenu() and perform necessary changes from there.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.