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
Super.Onbackpressed() and Finish() Exits the App
How to Create a Video from an Array of Images in Android
How to Get Data from Edit Text in a Recyclerview
Charging Animation for Android
How to Set the Preview Image in Videoview Before Playing
Recyclerview Is Cutting Off the Last Item
Getting the Current Fragment Instance in the Viewpager
Install_Failed_User_Restricted:Android Studio Using Redmi 4 Device
Android - Edittext Not Showing Any Text
How to Make Buttons Align At the Middle - Android Studio
How to Get the Full File Path from Uri
Error:Binderproxy@45D459C0 Is Not Valid; Is Your Activity Running
How to Implement My Custom Toolbar in My Fragment
How to Post Array in Retrofit Android
How to Disable Cache in Android Webview
Adb.Exe Not Found After Installing Android Studio
Android Fastboot Waiting for Devices
I Get Fragment Not Attached to a Context. What Context Need to Use