简体   繁体   中英

In my own custom android home screen, how do I catch the home button?

I know that android does not allow us to catch the home button press; however, I have my own built home screen replacement app and want to just know when the button was pressed to allow animations.

So, my app has just one activity as I do most of my "activities" as canvas drawing so I can have complete control over the visual aesthetics of my app. The problem is if the user navigates to a different page within the app and presses home, nothing happens, since the app is technically already in the same activity.

I want to know when the button is pressed so I can then animate/navigate my app back to the main screen. Also, I know I could accomplish this by having separate real activities however that won't work for what I'm trying to do.

You need to add <category android:name="android.intent.category.HOME"/> to your activity's intent filter in your app's Android.xml

You can take a look at "Home" app in Android SDK samples. They can be downloaded using these instructions .

//overriding method

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

        if(keyCode == KeyEvent.KEYCODE_HOME)
        {
           //The Code Want to Perform. 
        }
    });

You can detect when the home button is pressed because onPause() is always called. You are not allowed to override the home button, but the following should work

@Override
protected void onPause() 
{
    super.onPause();
    //code here
}

Of course the problem with using this is that it will be called even if the user is not quitting the app(ie. If they are sent to another application through an intent)

That is the closest that you are going to get to a onHomeButtonPressedListener there are really no ways to do this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM