Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. What is the correct syntax to call onCreateOptionsMenu() and onPrepareOptionsMenu() in Android Studio?

What is the correct syntax to call onCreateOptionsMenu() and onPrepareOptionsMenu() in Android Studio?

Scheduled Pinned Locked Moved Android
questionandroidgame-devtutorialannouncement
4 Posts 3 Posters 30 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    priyamtheone
    wrote on last edited by
    #1

    In Android Studio, I came across different ways to implement ``onCreateOptionsMenu()`` and ``onPrepareOptionsMenu()`` each with respect to the ``return`` statement in an ``Activity`` that extends from ``AppCompatActivity``. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application. ## onCreateOptionsMenu() #### Example 1: Only ``true`` is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } ``` #### Example 2: Call to super class is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } ``` #### Example 3: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } ``` As you see, in the third example, call to super class is done followed by returning ``true``. But in the first two examples, either ``true`` or call to super class is returned. Which one is the right way to do it? ## onPrepareOptionsMenu() #### Example 1: Call to super class is returned. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } ``` #### Example 2: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } ``` Here too the same question arises. Which syntax should I follow to implement the method properly?

    Richard DeemingR C 2 Replies Last reply
    0
    • P priyamtheone

      In Android Studio, I came across different ways to implement ``onCreateOptionsMenu()`` and ``onPrepareOptionsMenu()`` each with respect to the ``return`` statement in an ``Activity`` that extends from ``AppCompatActivity``. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application. ## onCreateOptionsMenu() #### Example 1: Only ``true`` is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } ``` #### Example 2: Call to super class is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } ``` #### Example 3: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } ``` As you see, in the third example, call to super class is done followed by returning ``true``. But in the first two examples, either ``true`` or call to super class is returned. Which one is the right way to do it? ## onPrepareOptionsMenu() #### Example 1: Call to super class is returned. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } ``` #### Example 2: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } ``` Here too the same question arises. Which syntax should I follow to implement the method properly?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Why not call the super class method first, and if it returns false, bail early? Eg:

      public boolean onPrepareOptionsMenu(Menu menu) {
      if (!super.onPrepareOptionsMenu(menu))
      {
      return false;
      }

      menu.findItem(R.id.action_save).setVisible(true);
      menu.findItem(R.id.action_reset).setVisible(true);
      menu.findItem(R.id.action_about).setVisible(true);
      return true;
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Why not call the super class method first, and if it returns false, bail early? Eg:

        public boolean onPrepareOptionsMenu(Menu menu) {
        if (!super.onPrepareOptionsMenu(menu))
        {
        return false;
        }

        menu.findItem(R.id.action_save).setVisible(true);
        menu.findItem(R.id.action_reset).setVisible(true);
        menu.findItem(R.id.action_about).setVisible(true);
        return true;
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        P Offline
        P Offline
        priyamtheone
        wrote on last edited by
        #3

        Thanks for your suggestion. I'll keep that approach in mind for future reference. However, I found a better way to deal with the situation. Unless my code relies on a superclass's implementation, returning ``true`` after my modifications is straightforward. If I ever extend a class and want to allow the superclass's logic to still apply, I'll use the ``super`` call then. That seems to be the optimal approach.

        1 Reply Last reply
        0
        • P priyamtheone

          In Android Studio, I came across different ways to implement ``onCreateOptionsMenu()`` and ``onPrepareOptionsMenu()`` each with respect to the ``return`` statement in an ``Activity`` that extends from ``AppCompatActivity``. It is quite confusing to me as to which one is the right way to implement the two methods, given that I want to maintain equality of syntax all across my application. ## onCreateOptionsMenu() #### Example 1: Only ``true`` is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } ``` #### Example 2: Call to super class is returned. ``` \@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu_activity_dashboard, menu); return super.onCreateOptionsMenu(menu); } ``` #### Example 3: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); Intent intent = new Intent(null, dataUri); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions( R.id.intent_group, 0, 0, this.getComponentName(), null, intent, 0, null); return true; } ``` As you see, in the third example, call to super class is done followed by returning ``true``. But in the first two examples, either ``true`` or call to super class is returned. Which one is the right way to do it? ## onPrepareOptionsMenu() #### Example 1: Call to super class is returned. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT > 11) { invalidateOptionsMenu(); menu.findItem(R.id.option2).setVisible(false); menu.findItem(R.id.option4).setVisible(true); } return super.onPrepareOptionsMenu(menu); } ``` #### Example 2: Call to super class is done in the first line and ``true`` is returned later. ``` \@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_save).setVisible(true); menu.findItem(R.id.action_reset).setVisible(true); menu.findItem(R.id.action_about).setVisible(true); return true; } ``` Here too the same question arises. Which syntax should I follow to implement the method properly?

          C Offline
          C Offline
          Carlos Jul2024
          wrote on last edited by
          #4

          To maintain consistency and ensure proper functionality, follow these guidelines when implementing onCreateOptionsMenu() and onPrepareOptionsMenu() in an AppCompatActivity:

          onCreateOptionsMenu()
          Use the first example where you return true after inflating the menu. This is the recommended approach because super.onCreateOptionsMenu(menu) in AppCompatActivity always returns true by default and doesn't perform any additional actions unless overridden.

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.game_menu, menu);
          return true;
          }
          onPrepareOptionsMenu()
          Use the second example where you call super.onPrepareOptionsMenu(menu) first and return true later. This ensures any default behavior is executed, and returning true indicates the menu was successfully updated.

          @Override
          public boolean onPrepareOptionsMenu(Menu menu) {
          super.onPrepareOptionsMenu(menu);
          menu.findItem(R.id.action_save).setVisible(true);
          menu.findItem(R.id.action_reset).setVisible(true);
          menu.findItem(R.id.action_about).setVisible(true);
          return true;
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups