I am working on an app in which I have one dialog and when I click on the exit button I want to close the app but sometimes app won't finish and return back to my first activity. I do not understand what to do with this.
Code for the same
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
alertDialog.setMessage(context.getResources().getString(R.string.app_close_dialog_msg));
alertDialog.setPositiveButton(R.string.app_close_dialog_msg_yes, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismiss();
((Activity) context).finish();
//((Activity) context). moveTaskToBack(true);
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
}
});
Don't
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
Do
Intent _intentOBJ= new Intent(Intent.ACTION_MAIN);
_intentOBJ.addCategory(Intent.CATEGORY_HOME);
_intentOBJ.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
_intentOBJ.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(_intentOBJ);
Try calling ActivityCompat.finishAffinity(activity); if your minSdk >= 16, this should close all the activities for your app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With