Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:24:54+00:00 2026-06-15T15:24:54+00:00

My android application targets the latest platform. I am new to the platform, and

  • 0

My android application targets the latest platform. I am new to the platform, and read bit conflicting information on actionbar. The way I was using it for navigation was.

menu.xml

<menu>
            <item android:id="@+id/action_sort_size"
                  android:icon="@android:drawable/ic_menu_sort_by_size"
                  android:title="@string/action_barabc"
                  android:onClick="abc" />
            <item android:id="@+id/action_sort_alpha"
....

In my activity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
public void abc(MenuItem item) {
//...
}

this works, but the back/up navigation is not working correctly. could be unrelated, still like to confirm.

But, I also see implementation like here
where it switches on item.

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuitem1:
      Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
          .show();
      break;
    case R.id.menuitem2:
....
}

Which is the better approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:24:55+00:00Added an answer on June 15, 2026 at 3:24 pm

    The better approach, in my opinion, is the switch approach. There aren’t many reasons why, but I’ll list them:

    • The code is centralized. You don’t have x amount of methods that basically do the same thing. It keeps your code more readable; it is “cleaner”. You also get a default statement using the switch, this can help if you mess up and forget to make a case specifically for an element in the layout.
      If you really wanted to have a centralized method using xml, you would have onClick reference the same method and check the ids of the View parameter. Which is essentially the same asonOptionsItemSelected.
    • It is a part of the API. The Android engineers would not have made it a part of the API if they didn’t want the developer to use it. Yes the XML is techinally API, but XML should be used more for layouts and visuals, not for logic.
    • Everyone uses it. All the tutorials I have seen and everyone’s code uses this method. It is now more of a convention.

    It’s largely personal, but if it looks like it’s a convention, and everyone uses it, I’d adhere to it. Especially if you’re working as part of a team. Different coding styles for such arbritrary things should be avoided.

    And concerning your back/up navigation, it shouldn’t make a difference which way you do it, since you have to implement the same code to get that navigation type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a simple Android application using NDK. Here are the contents
I am trying to run an Android application on a BlackBerry PlayBook Simulator using
I have android application that I need to debug using eclipse. When I right
My application targets the v10 of the android Sdk but has the v6 for
I am using vs-android to compile the sample application available on their site. I
I'm testing my android application using calabash-android which provides it's own "test project", with
I am developing an application that targets multiple devices, iOS, Android and WP7. For
I write an android application that send text data to asp.net webserver. I'm using
I am working on an Android application. In my app I am using a
I am using Robotium to test android application. Below is my piece of code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.