I have a problem with my apps,i want to close my app when clicking the back button. But When clicking back from this screen, application back to prev screen. I try to add all exit script, but not work, Help Please. My Script Is :
public void onBackPressed() {
Exit = new AlertDialog.Builder(tab1.this).create();
Exit.setTitle("Exit Program");
Exit.setMessage("Exit?");
Exit.setButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
Intent intent = new Intent();
System.exit(0);
finish();
System.runFinalizersOnExit(true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Exit.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
Exit.dismiss();
}
});
Exit.show();
}
// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Exit = new AlertDialog.Builder(tab1.this).create();
Exit.setTitle("Exit Program");
Exit.setMessage("Exit?");
Exit.setButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Progress
android.os.Process.killProcess(android.os.Process.myPid());
Intent intent = new Intent();
System.exit(0);
finish();
System.runFinalizersOnExit(true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Exit.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
Exit.dismiss();
}
});
Exit.show();
return true;
}
return super.onKeyDown(keyCode, event);
}