Skip to content Skip to sidebar Skip to footer

How To Finish All Activities And Close The Application In Android?

My application has the following flow: Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3 My problem is that when I am

Solution 1:

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

Solution 2:

This works well for me.

  • You should using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags.

    Intentintent=newIntent(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.

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        finish(); // Exit 
    }
    

Solution 3:

Use finishAffinity() method that will finish the current activity and all parent activities. But it works only for API 16+ mean Android 4.1 or higher.

API 16+ use:

finishAffinity();

Below API 16 use:

ActivityCompat.finishAffinity(this); //with v4 support library

To exit whole app:

finishAffinity(); // Close all activites
System.exit(0);  // Releasing resources

Solution 4:

Sometime finish() not working

I have solved that issue with

finishAffinity()

Do not use

System.exit(0);

It will finish app without annimation.

Solution 5:

To clear all the activities while opening new one then do the following:

Intent intent = newIntent(getApplicationContext(), YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Post a Comment for "How To Finish All Activities And Close The Application In Android?"