Android Prevent Back Button from Closing the App

how to set Back Button , closing activies except main activity in android?

Here is your answer try with this way.
For your 1st Activity onBackPressed() method write below code.

@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
NumberOne.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}

For others activity use below code.

    @Override
public void onBackPressed() {
finish();
}

Prevent Back button from closing my application

Have you tried putting the super call in an else block so it is only called if the key is not KEYCODE_BACK ?

/* Prevent app from being killed on back */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

// Back?
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Back
moveTaskToBack(true);
return true;
}
else {
// Return
return super.onKeyDown(keyCode, event);
}
}

In all honesty though, you can't rely on this because once your app is placed in the background, at any moment it could be recycled for the system to reclaim memory.

Cordova - Prevent back button from closing app on Android

Hi Please try below code :

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
document.addEventListener("backbutton", function (e) {
e.preventDefault();
}, false);
}

This should work.

How to prevent Android from closing web-application when backbutton is pressed?

I solved my own question by adding the code below to the DefaultActivity.java file to prevent the default android behavior, and keeping the JavaScript code as stated in the question:

@Override
public void onBackPressed() {
return;
}

I hope this helps someone in the future with the same problem!



Related Topics



Leave a reply



Submit