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.
Related
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!
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
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
Home key press Event Listener
In my app I want to make a change that at any point of time i.e at any activity I press Home button it shows a dialog box asking for "You want to logout?" .
I have read that overriding of Home button is not possible.
Is that true?
Any ideas?
hi you can't handle Home button in android as per to my knowledge. but yes you can do like this follow this Catch keypress with android
There is no direct way to do the same. like you can not get the KeyDown event for the HOME key. but we can get the HOME key captured and stay in the same screen by following code
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
You can override the "back" button but you cannot override the Home button. It is the user's "ctrl+alt+del" for 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