Answer the question
In order to leave comments, you need to log in
How to prevent quitting an app with the back button in Android?
Hello, the tablet is Android 5.0.1 (Asus TF300T)
I need to disable the exit from the application with the back key, I work in the Android Studin 1.0.1 environment. I put
all the code along the path Project-> java-> com.test.project-.MainActivity
Code
@Override
public void onBackPressed()
{
moveTaskToBack(true);
}
Does not work@Override
public void onBackPressed()
{
super.onBackPressed();
}
Also doesn't work. Answer the question
In order to leave comments, you need to log in
Working and final version:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
// Don't close application
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
@Override
public void onBackPressed(){
Toast.MakeText(getApplicationContext(),"You Are Not Allowed to Exit the App", Toast.LENGTH_SHORT).show();
}
Something I didn’t understand at all
Here is the code that is responsible for pressing the back key
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
And here is the code which, as I understand it, returns false on any keystroke@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
But it works exactly the same as the upper code, everything else works (Folding, etc.) how to understand this? Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question