I want to block home button,back button and minimize button in navigation bar. Is it possible to block this button in android version 4.4.2?
Normally you're not supposed to block the usage of the Home button. Users should always be able to exit your app through the use of the home button. There is no api to disable this button.
1) If it is really necessary to block this button, you can make a custom OS with that functionality. There are most likely better solutions though.
2) You could register your app as a launcher app. This way you will know when the user presses the Home button and can act accordingly.
You can override the functionality of the back button.
Override onBackPressed in your activity.
I don't know what you mean with the "minimize button".
#Override
public void onBackPressed()
{
super.onBackPressed();
}
in this code remove super.onBackPressed(); line.
To remove back button.
Related
I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}
I want to disable back button from closing the app.
How can i disable back button?
You can override the onBackPressed() method and determine what happens in that method. Like this:
#Override
public void onBackPressed() {
// do nothing because you don't want them to leave when it's pressed
}
Just add that method to your activity class and you're good to go.
However, this is bad app design. What you would most likely want to do is make a dialog pop up that asks them if they are sure they want to leave. You would add the dialog code inside that method so that when the back button is pressed, the dialog pops up.
Generally, that's not a good idea. Users hate to feel "trapped" in your app.
Many users are able to start apps "on top of" other apps. When they hit "back" they may expect your app to stop, and the app they were in previously to appear. This is different from "home" where they expect all apps to go to the background.
Users familiar and comfortable with this functionality will not like it if you change "back" - although you may give them options like "press back again to exit" as some apps do. It depends on your particular situation.
So if you are in need of it, here is a good reference:
Android - How To Override the "Back" button so it doesn't Finish() my Activity?
I want to show alert when user presses home button on device(do you want to exit the app ?)
How to do it ?
You can't handle Home button click, because of Android policy (Home button click event handling android). Of course, you can use onPause()/onStop() method of your current Activity, but your application will be moved to background too quick and user will not see your dialog, I think.
Also, note that Home not closes the app - just moves to background. User usually close app by pressing Back on main activity, try to handle it:
#Override
public void onBackPressed() {
// your dialog here
}
Is is not possible for Android apps to override the functionality of the home button. The best you can do is show the dialog when the user presses back in your topmost Activity.
You can find more information at the following SO answers:
https://stackoverflow.com/a/7240268/3214339
Android Overriding home key
Write the show alert dialog code in onPause() it will work perfectly.
When you press the home button, what does it do by default?
I want to keep what it does by default, but make sure it ends my music as well.
For example:
public void onBackPressed() {
return;
I disabled my back button.
I want to make it so the home button does what it does, but i want to call my
this.stopService(new Intent(this, Music.class));
in the method too.
It is not possible to override "Home" key press just like you did for Back button press. In fact you're not supposed to over ride home key press because user presses Home key to quickly come to the launcher.
Even if you try to override onKeyDown(), you'll see that Home key press will not invoke this function.
But pressing Home key will surely call onStop() of your activity and hence you can stop your music service in onStop(). Hope this helps.
When you press the home button, what does it do by default?
It brings the home screen activity to the foreground.
I want to keep what it does by default, but make sure it ends my music as well.
Stop the music in onPause() or onStop() of your foreground activity, as these will be called when it loses the foreground to the home screen activity.
If you are talking in coding sense -
The home button brings the default launcher to the foreground.
Call onPause() or onStop with super.onPause() or super.onStop() inside of the method.
If you are talking in literal terms with nothing to do with code then..
The home button launches the default launcher activity, this can be cleared if you installed the launcher on some devices by going settings > applications > manage find the application and then go clear defaults
When the music stops is decided by the developers code, you cant change how it works sorry.
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!