how to finish all activities and close the application in android?

There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.


This works well for me.

  • You should using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags.

    Intent intent = new Intent(SecondActivity.this, CloseActivity.class);
    //Clear all activities and start new task
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);
    
  • onCreate() method of CloseActivity activity.

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        finish(); // Exit 
    }