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

  • Home
  • SEARCH
  • 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 7535985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:27:03+00:00 2026-05-30T06:27:03+00:00

Today I amazed that why my menu doesn’t show when i put onKeyDown() in

  • 0

Today I amazed that why my menu doesn’t show when i put onKeyDown() in my code!!!

in my project i have menu and i designed it with onCreateOptionsMenu() and onOptionsItemSelected() in regular way. The project doesn’t have problem and works fine.

The problem is using onKeyDown(). when i put it into my activity and run the project, when i click on menu button, it doesn’t show anything. while when i comment out onKeyDown() the app works fine.

What is the reason? Thank you

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.home:
            Log.i("Menu", "Home clicked.");
            return true;
        case R.id.social:
            Log.i("Menu", "Social Networks clicked.");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);

    if(keyCode == KeyEvent.KEYCODE_HOME){
        Log.i("onKeyDown", "Home presed");
        int pid = android.os.Process.myPid(); 
        android.os.Process.killProcess(pid); 
    }

    if(keyCode == KeyEvent.KEYCODE_BACK){
        Log.i("onKeyDown", "back presed");
        int pid = android.os.Process.myPid(); 
        android.os.Process.killProcess(pid);
    }
    return true;
}
  • 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-05-30T06:27:04+00:00Added an answer on May 30, 2026 at 6:27 am

    When you return a value from the onKeyDown() method, it indicates whether the event has been handled or not. In your case true is always returned regardless of whether you have really responded to the event or not. The correct version of the method should be:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_HOME:
                //Handle the event
                return true;
            case KeyEvent.KEYCODE_BACK:
                //Handle the event
                return true;
        }
    
        return super.onKeyDown(keyCode, event);
    }
    

    Also, I should say that killing the process on the button clicks is not how such things are handled in Android, you should manage your activity instead. Back button finishes activity by default, and you should leave this as that, restarting activity on launch is specified in AndroidManifest.xml.

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

Sidebar

Related Questions

Today while I was writing some code for two methods that shows and hides
Today I have encountered a problem that required me to determine the maximum index
Today I was listening to the Hanselminutes show about .NET 3.5 SP1...What's inside ,
Today I discovered that my fresh installation of Apache HTTP Server is able to
Today I had a coworker suggest I refactor my code to use a label
Today at work we came across the following code (some of you might recognize
Today somebody told me that interface implementation in C# is just Can-Do relationship, not
Today we have received some strange exceptions on our production website. They all have
Today, I got completely surprised when I saw that a global variable has undefined
today i'm having a little problem, that probably is nothing for pros here :)

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.