How to handle KeyEvent of home button in an Android application? - android

I'm developing an application in Android, and I use the the following code to handle the KeyEvent for the back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
How would I go about doing this for the home button?

You can not control the behaviour of Home key. You will not get the event of Home key but you can disable it but it is highly recommended you should not do this.Before blocking the Home key refer this post.
However you can block the home key like this:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

As this question suggests: Android Overriding home key this is unfortunately not possible. Perhaps there is some other way to implement your desired behavior?

Obvious answer is - handle home key press in onPause() method of your activity. This callback is called when user hits home key. Guaranted. Home key is not for you, but
for OS and user.

Read these :
Overriding the Home button - how do I get rid of the choice?
Android - How to catch that the Home button was pressed?
The home button is supposed to do one thing and one thing only and consistently. Get the user back to the the HOME screen. Even if you could override it's behavior it would be an extremely user-unfriendly thing to do. So don't do it and solve your problem differently!strong text

Related

How to change Home button functionality in Android

I am developing an application in API level 2.2. In my application whenever I press Home button it forces my app to exit, and when I relaunch that app by clicking on its icon, it starts from where it exited. I have tried to change/ override functionality of Home button but it does not works.
I have tried as
`
public boolean onKeyDown (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
return false;
}`
But it works for Back button only and not for Home button.
I have also tried to get keyCode of Home key but I didn't get it.
I need solution to change Home Button functionality.
Simply put: better you do not change the behavior of your home button...
Instead of trying to reprogram your Home button, you could try using some of the Activity callbacks that Android provides. You can take a look at the Activity lifecycle here:
http://developer.android.com/reference/android/app/Activity.html
For example, you could try overriding the onPause callback in an effort to achieve the effect you're going for.
#Override
public void onPause() { /* do stuff */ }
In your case, do stuff might be returning the application to its initial state or forcing it to exit. I've never attempted a force quit myself, but there are some answers on this StackOverflow post:
How to exit from the application and show the home screen?
Best of luck!

Restrict the home button Android 4.0 +

I am working in an launcher application ,In which i have to disable the home button event.
How can we do this in android 4.0 and above.
There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit. The home button is the one sure shot way to be able to leave any app.
If you want to handle the HOME button, implement a home screen.
For more information check the link below. check the answer by commonsware
Not able disable Home button on specific android devices
Here you have ;)
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME)
{
return true;
}
return super.onKeyDown(keyCode, event);
}
You should use this in your activities.
You could use the method onPause() onPause
So if you press Home and return to you app you can run whatever you want.
Maybe it helps

Android :Get Key press, Key release Events out of Activity Screen

I wanted to get Get Key press, Key release Events out of my Activity Screen
i.e ex When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
When i am in Android Native Home Screen and if i Press Search KEY i need to fire my API..Is it Possible??
No. Key events are generally only delivered to the foreground activity. The two exceptions are the CAMERA button (on those few devices that have one) and the MEDIA button (found on headsets) -- those are sent as broadcast Intents if the foreground activity does not consume the event.
I don't know about the SEARCH button, but BACK button you can override.
As I know HOME button can't be override.
Maybe consider using MENU button to display option menu?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//your method
return false;
}
return false;
}

Override home button behaviour

I am working on an app and i want it to be such that when the user presses home then nothing should happen. Is it possible to accomplish that? If that is not possible then is it possible that some other action is performed when hone is pressed. the whole idea is user should not leave the app directly from any screen.
update:can someone tell me how to define my app as launcher?
No, you cannot. Whenever Home button is pressed, the framework will always throw you back on android's home screen. Sorry, you are out of luck. :)
you cant make it work as you want, but you can disable it
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
and you can tell user that home button is disabled:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context,"Home button is disabled",1);
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
I am working on an app and i want it to be such that when the user presses home then nothing should happen. Is it possible to accomplish that?
Make your app be the home screen. The user can still remove your app by rebooting in safe mode.

Start progress bar on Home key button click

How can I start a progress dialog when the default Home Key button of Android is pressed? If it's possible, then how?
I've tried using the onKeyDown method, but it doesn't work.
Not really, no. You cant override the home button as you would the back or search.Take a look at this discussion:
Overriding the Home button - how do I get rid of the choice?
Duplicates this: Android - capture/suppress Home and EndCall buttons events?
The short answer: you can not handle Home button, but you can write a replacement Home Screen application.
Because the 'Home' button is such an essential feature to android (It provides the user with a quick one-touch button to get back to their home screen) that they made it so we can't run custom code to modify what it does. We do however, somewhat have control over what happens when the user presses the menu, back, or search buttons.
It's always best to conform to Android's UI design guidelines, this article on application design is a great read on how exactly we should go about things.
The prevailing wisdom says it cannot be done.
Checkout this conversation:
Home key press Event Listener
Maybe the below code would work the way you want it to. But I don't think you can trap the Home key absolutely.
Override below method in your activity.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
Then after you can listen home key in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{}
});
Hope this may helpful to you.

Categories

Resources