0

According to the Android menu documentation and a similar question, onPrepareOptionsMenu should be used to update menus dynamically. For Android 3.0 and higher, the documentation says:

When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu()

However, when calling invalidateOptionsMenu(), the system calls both, onCreateOptionsMenu() and onPrepareOptionsMenu(). Resuming the activity also either calls both methods (if it was stopped in background) or none.

To me, this sounds like the differentiation between the two methods is obsolete. Is there any situation in which Android only calls onPrepareOptionsMenu() but not onCreateOptionsMenu()? Additionally, is there a way to manually request that Android only calls onPrepareOptionsMenu(), like it is implied in the documentation (cited below)?

[onCreateOptionsMenu()] is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).

Thank you very much!

ByteHamster
  • 4,884
  • 9
  • 38
  • 53

1 Answers1

0

You are using Activity menu here, so assuming you have single activity app it will be only inflated once, during onCreateOptionsMenu - that's the scenario with a single call.
If you want to change it, then you have to invalidate the current one and inflate a new one - as it's an app's global menu it only make sense if you want to change its contents - you do that using onPrepareOptionsMenu and then inflate it again in onCreateOptionsMenu call.

But if you make lots of menu changes then it amy be better to use to use Fragment specific menus - they only live as long as the their fragment - menus

So to answer your specific question - you cannot NOT trigger the onCreateOptionsMenu. You can override it to do absolutely nothing but then the menu will not be inflated

EDIT - it seems I misunderstood the documentation as well. The key bit for onPrepareOptionsMenu is:

This is called right before the menu is shown, every time it is shown.

Which in practice means that every time you click on the Menu onPrepareOptionsMenu will be called, without calling onCreateOptionsMenu

Stachu
  • 1,614
  • 1
  • 5
  • 17
  • I wrote this question focused on Activities but the same behavior applies to Fragments (when having to change their items dynamically, for example a download indicator). The scenario you are describing above is not using a single call. `onPrepareOptionsMenu` is called there, too. My question basically is: "if you cannot NOT trigger `onCreateOptionsMenu`, why does the Android framework provide two functions and always calls both instead of just having a single one?" – ByteHamster Aug 07 '20 at 11:23
  • Because they represent different stages in the menu lifecycle, and therefore serve different purposes. It's basically the same as with e.g. `onCreateView` and `onViewCreated` - they represent a step in the lifecycle and depending on what you want to do, you can use the appropiate method – Stachu Aug 07 '20 at 11:31
  • For `onViewCreated`, they explicitly state that it is always called after `onCreateView`. For menu handling, they state something different. To me, the documentation sounds like there is more to it than just lifecycle methods that are always called after each other: `[onCreateOptionsMenu()] is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)`. Does this mean that the documentation is wrong/misleading? – ByteHamster Aug 08 '20 at 09:26
  • Sorry - I did not notice that you edited your answer. `every time you click on the Menu onPrepareOptionsMenu will be called, without calling onCreateOptionsMenu`: Clicking items does not seem to trigger the `onPrepareOptionsMenu` call for me. Does it for you? – ByteHamster Oct 14 '20 at 11:20
  • 1
    it does, I've monitored it using Log.d in API 29 – Stachu Oct 14 '20 at 12:09