I want to close application when I press device's back button.I am using this code..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the onBackPressed() in your Activity where you want application to Quit when the device back button clicked
#Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This code does it :
public void onPause() {
super.onPause();
finish();
}
Don't forget to mark it as answer if it helped ;)
The answer that you marked as correct will stop the app every onPause of this activity, which is not an event of the back button cick, the right answer is :
#Override
public void onBackPressed() {
finish();
}
Related
I'm trying to make my app not close when the user pressed the back button and remain in the same state at least for a while, like the home button. I searched and some people suggested to do this.
#Override
public void onBackPressed() {
super.onBackPressed();
this.moveTaskToBack(true);
}
but this didn't help. I have to mention the activity I'm using this code on is not the LAUNCHER activity and before this activity, there is a splash screen.
what am I doing wrong?
Try my code in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown: ");
if(keyCode == KeyEvent.KEYCODE_BACK){
goHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void goHome() {
Intent i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
Do start the home screen when key back down.
You should not call super method in onBackPressed(). It should be simply like this
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
I am Using following method to disable HomeKey click event ,but after Clicking Home key My App will be closed. I want ,whenever user click home key my app will not be close.
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
moveTaskToBack(false);
}
Since Android 4 there is no effective method to deactivate the home
button. That is the reason why we need another little hack. In general
the idea is to detect when a new application is in foreground and
restart your activity immediately.
Check this workaround: http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/
Override the below method in your Activity,
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
Put his into your activity's code. It will intercept back button click
#Override
public void onBackPressed() {
}
u can ovverride home button pressed like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
I am able to detect when the back button is pressed by using:
#Override
public void onBackPressed() {
//Do my thing
}
But now I want to detect when the back button is released. How can I do that? I'm unable to find any method like onBackReleased() which I can override to do my thing.
There is no method that specifically detects the back button release, but you could use the onKeyDown() and onKeyUp() methods. Check to make sure the key is the back button, and do your stuff in the method. Here is something you could do:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do your stuff for when the back button is initially pressed
return true;
} else {
return false;
}
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do your stuff for when the back button is released
return true;
} else {
return false;
}
}
If you wanted to override the default back press, you could just use the onBackPressed() method, like so:
#Override
public void onBackPressed() {
// Prevents the user from clicking the back button and returning to the parent activity
}
These methods can be implemented directly in your class. Hope it helps!
How to disable back button pressed for webview in android ?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (wv1 != null && (keyCode == KeyEvent.KEYCODE_BACK)
&& wv1.canGoBack() )
{
wv1.goBack();
}
return true;
}
If you want to disable back button action when the WebView Visible and enable back button action if the WebView in not Visible try the below code in your Activity
#Override
public void onBackPressed() {
if(webview.getVisibility()==View.VISIBLE){
// dont pass back button action
if(webview.canGoBack()){
webview.goBack();
}
return;
}else{
// pass back button action
super.onBackPressed();
}
}
Simply override the onBackPressed() method.
#Override
public void onBackPressed() { }
Please try this
#Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
}
else{
super.onBackPressed();
}
}
There are many ways to make it,
Solution 1, overriding dispatchKeyEvent()
dispatchKeyEvent()(API Level 1, Android 1.0)
Refer to my answer use dispatchKeyEvent to disable back button
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.dispatchKeyEvent(event);
}
Solution 2, overriding onBackPressed()
onBackPressed() (API Level 5, Android 2.0)
Refer to Use onBackPressed() to disable back button
#Override
public void onBackPressed() {
}
Solution 3, overriding onKeyDown()
onKeyDown() (API Level 1, Android 1.0)
Refer to Use onKeyDown() to disable back button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
You have add below code in Activity for disable activity back pressed
#Override
public void onBackPressed() {
}
Add this below code in java file :
WebView mwebView;
Button backButton1;
backButton1 = findViewById(R.id.backButton1);
mwebView = findViewById(R.id.mwebView);
backButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWebView.destroy();
}
});
Is it possible to change this code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event);
}
so that when I click on back button it works like a home button?
try this,
#Override
public void onBackPressed() {
Intent backtoHome = new Intent(Intent.ACTION_MAIN);
backtoHome.addCategory(Intent.CATEGORY_HOME);
backtoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(backtoHome);
}
Adding this to your Activity, will make it look like your app is responding to a Home Button Click event
One line solution
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
And also see this
#Override
public void onBackPressed()
//super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}
If something doesn't work like expected try it with uncommented
super.onBackPressed();