Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App does not close after calling finish() in android

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());



            }
        });
like image 735
Android Developer Avatar asked Oct 26 '25 10:10

Android Developer


2 Answers

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);
like image 75
IntelliJ Amiya Avatar answered Oct 28 '25 23:10

IntelliJ Amiya


Try calling ActivityCompat.finishAffinity(activity); if your minSdk >= 16, this should close all the activities for your app

like image 42
Muddassir Iqbal Avatar answered Oct 28 '25 23:10

Muddassir Iqbal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!