Search code examples
androidexitback-button

Android: Quit application when press back button


In my application i want exit from app when press back button, this my code:

@Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure you want to exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("No", null).show();
    }

it's work correctly but when i exit from app it does not exit completely and show empty page with my app logo and when i again press back button exit from app, How can i fix it???

EDIT :

I use this code instead of above but my app exit completely but i want it running at background and does not exit completely , how can i do it?

@Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure?")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                }).setNegativeButton("no", null).show();
    } 

Solution

  • When you press back and then you finish your current activity(say A), you see a blank activity with your app logo(say B), this simply means that activity B which is shown after finishing A is still in backstack, and also activity A was started from activity B, so in activity, You should start activity A with flags as

    Intent launchNextActivity;
    launchNextActivity = new Intent(B.class, A.class);
    launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);                  
    launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(launchNextActivity);
    

    Now your activity A is top on stack with no other activities of your application on the backstack.

    Now in the activity A where you want to implement onBackPressed to close the app, you may do something like this,

    private Boolean exit = false;
    @Override
        public void onBackPressed() {
            if (exit) {
                finish(); // finish activity
            } else {
                Toast.makeText(this, "Press Back again to Exit.",
                        Toast.LENGTH_SHORT).show();
                exit = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        exit = false;
                    }
                }, 3 * 1000);
    
            }
    
        }
    

    The Handler here handles accidental back presses, it simply shows a Toast, and if there is another back press within 3 seconds, it closes the application.