This question already has answers here:
Disable back button in android
(18 answers)
Closed 9 years ago.
Is their a way of disable a BACK button on my mobile? because while i'll transfer it to my mobile the BACK button is activated so that it can back the previous page. Please help me to disable BACK button while my app is running.
Override the onBackPressed() of your activity
#SuppressLint("MissingSuperCall")
#Override
public void onBackPressed()
{
// super.onBackPressed(); // Comment this super call to avoid calling finish() or fragmentmanager's backstack pop operation.
}
You may suppress lint's error by adding #SuppressLint("MissingSuperCall") as per Matthew Read pointed out.
This way works for all versions of Android, and will work with libraries that may override the default functionality in their Activities (such as cocos2d-x):
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return (keyCode == KeyEvent.KEYCODE_BACK ? true : super.onKeyDown(keyCode, event));
}
This is very simple.. put this code in every activity of your android code if you wish to disable the back button for complete aplication
#Override
public void onBackPressed()
{
//thats it
}
and you are done
Related
I have a certain activity that begins when I tap my smart watch's screen. There is a timer and bunch of stuff that happens, but the process is crucial, so I am handling certain cases or things that might happen that would disturb the flow of things.
So basically, I want to prevent the home button of my watch to exit the app and go to the homescreen while my timer is running. I keep looking this up and most people say to override the onBackPressed method. But this was for the back button, and I I realized the button is a home button not a back button.
frameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
clicked = clicked + 1;
if (clicked == 2)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
startTimer();
}
else if (clicked >= 4)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
AlertMessage();
}
}
return true;
}
});
this is the main method I use
Just override the onBackPressed function.
#Override
public void onBackPressed ()
{
//Control the flow
}
Use third part library in case if it solves your problem.
Here is the link:
https://github.com/shaobin0604/Android-HomeKey-Locker
The general consensus is that you can't override the home button behavior on a Wear OS device, just like you can't override the home button on an Android phone. This is by design to prevent developers from preventing the user to leave an application. Even if there is a hacky way to do it, this is not officially supported and may stop working at any time in future OS versions. I highly suggest not doing it since it goes against the basic navigation model of the device.
More details, and some workarounds for common use cases where people think they need to override the home button can be found in this blog post.
I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.
From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29
I am learning app development from a book, and have run into a problem with the avd. I have my code set up to change some text when the right arrow key is pressed:
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
textUpdate();
return true;
}
return false;
}
When I run the virtual device and press the arrow keys nothing happens. When I press the arrow keys on my physical keyboard, still nothing. I have done a lot of research and can't find the solution. I have tried editing the avd settings and editing the device(Nexus One) itself to accept keyboard and dpad input. What should I try now?
Have you overriden the method?
There should be an #Override above that function.
Override calls that function whenever its triggered so you can modify it to your specific implementation.
Hope this helps
the "arrow key" means the back key on android device?
if it is then you should override the onBackPressed
#Override
public void onBackPressed() {
// do something
}
Alright, what happened was my onKeyDown() function was inside a Fragment. I don't know why, but when I moved this code into the mainActivity it worked fine. If anyone has an answer as to why this worked and how I could get it to work in the fragment please comment.
Thanks
Hie team gud eve, I had a set of images like 1 2 3 4 5 6. on clicking on 1 (i.e first image) the one should show in another activity. When we swipe with the finger it should move to the next image. If i stop swiping near the image position 4. while pressing the back button on the device. The main Activity should show near the position of 4.
I already done with the swipe with the help of onFling(). The problem is when i press the back button the image is moving to the first position.I need in the same position......
Thanks in advance
manually you can override that key
i think this code will help u...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
try {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
//start the what activity you want
}
}
If you're targeting Android 2.0 or newer, you can use onBackPressed() to decide what happens when you hit the back button. This is slighlty easier to do.
#Override
public void onBackPressed() {
// Do something
}
To select a item in the Gallery:
gallery.setSelection(int selection);
I have an activity which has multiple screens depending on which buttons the user clicks.
What should I do if I need to handle back button in this activity. i.e. When I press to back button it has to go previous screen of my activity.
I am really new to android. can any body help me to solve this problem
Thanks for reading.
I guess that by multiple screens you mean you have some layouts and change them with setContentView(). You'll have to override the back button's behavior, keep a history of user navigation between various screens (if there's no forced path) and have the back button code set content to the previous screen.
Overriding the back button is easy if you're on API >= 5: see onBackPressed().
If you want also backward compatibility you'll find something here and here.
As this is usually all done automatically by Android with activities, consider having multiple activities instead of a single activity with multiple screens.
override onBackPressed() function in your activity and write the desired code in it for the intent firing.
You can do this by handling the KeyDown event and adding some condition( like taking a static variable and at each activity assign different value to the static variable ) at each actvity.
See the following code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(condition 1)
{
Intent i=new Intent("com.prac.A"); // A is your another activity in com.prac package
startAcitvity(i);
}
else if(condition 2)
{
Intent i=new Intent("com.prac.B");
startAcitvity(i);
}
else
{
Intent i=new Intent("com.prac.C");
startAcitvity(i);
}
}
return false;
}
Hope this will help you.