I was wondering if I can override the action of the back and home button is some cases. Normally these buttons should just react like they always do, but in a case some setting is true I want to override the buttons and let them call my own methods.
I´m using these two methods to override these buttons:
@Override
public void onBackPressed() {
// call my backbutton pressed method when boolean==true
}
@Override
public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
// call my homebutton pressed method when boolean==true
}
You call super.onBackPressed() to call the normal method. Exemple :
Regarding overriding the behaviour of Home Button you are out of luck.
However if your app is a specific one and have limited targeted audience, like inter-organization app, hospital kiosk, restaurant ordering, you can try making your app as Home (the launcher app). You can find a good example here: How to Write Custom Launcher App in Android
And to override the back key events, lot of examples are there.
For example:
Yes you can do override
Home
button.I have developed an application which disable hard button, you can have a look. I have taken a toggle button which locks all hard button to work except Power button
main.xml
make your boolean variable member variable
I use this:
When I am in a other screen (other then menu screen), I set the variable screen to 1. When I press the back button, it goes back to the menu screen (instead of killing the app). and give the screen variable the number 99, then when you hit the back button again, it kills the app.
However, you can't change the home button.
No you can not. What you can do is to ovveride the method and manage the boolean inside it:
for instance: