Prevent the closing of Application on click back button
Here we go for a simple code which will prevent your application from closing on click backbutton [Hardware-Android phone].
Thus the code begins :
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
AlertDialog.Builder alertbox=new AlertDialog.Builder(Firstpage.this);
alertbox.setTitle("Warning");
alertbox.setIcon(R.drawable.info);
alertbox.setMessage("Exit Application?");
alertbox.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alertbox.setNegativeButton("No", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
AlertDialog.Builder alertbox=new AlertDialog.Builder(Firstpage.this);
alertbox.setTitle("Warning");
alertbox.setIcon(R.drawable.info);
alertbox.setMessage("Exit Application?");
alertbox.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alertbox.setNegativeButton("No", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
Thus it will generate alertbox ,onclick yes it will close the app:
5 comments:
When i clicked yes it didn't terminate the application.what can i do?
For complete exit an application you can try like this..
int pid=android.os.Process.myPid();
android.os.Process.killProcess(pid);
Or you may do like this
this.finish();
Hi, i use this code on my 2nd page layout and when i back pressed it going back to previous which is layout 1 instead of closing completely the app.. Any suggestion for how i should prevent it from going back to the previous page?
i face this problem in my phone real estate turkey
Post a Comment