Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Finishing Up

onCreateOptionsMenu vs onPrepareOptionsMenu

According to documentation: "If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items." Why are we updating states in onCreateOptionsMenu? UPDATE: so this question is actually in response to Create an Options Menu workshop by Ben Deitch. In this workshop, he mentions that whenever the state of a checkbox from the menu option is changed, we should do it within the onCreateOptionsMenu, so my question is, why not in the onPrepareOptionsMenu? Does it matter where we update, or is this more like a best practice sort of thing? I've tried it in both places, and in both cases they work.

1 Answer

Ivan Penchev
Ivan Penchev
13,833 Points

Question is not very well asked. But think about the flow.

onCreateOptionsMenu() is called once. onPrepareOptionsMenu() is called every time the menu opens.