Normally when we launch new activity, it’s previous activities will be kept in a queue like a stack of activities. So if you want to kill all the previous activities, just follow these methods.

Method 1

In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.

Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags

Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.

  • Virender Dall

    not getting Intent.FLAG_ACTIVITY_CLEAR_TASK

    • rahul


  • Rorisang

    How about for api levels lower than 11, in particular 8 going up

  • Venugopal Naidu

    HI how can i clear the stack using fragment

  • You can set android:noHistory=”true” on activity tag at AndroidManifest.xml

  • mariano zorrilla

    THANK SO MUCH! I’ve been fighting with this SO much… this ones the ONLY correct answer I’ve found!

  • MjnRaja

    simply add finish() after startActivity()

  • Abhilash .K.V

    for Api level 8

  • riddhi shah

    what if i need to maintain activity stack ??

  • shrikant chaudhari

    Does this is the good way to finish current activity after calling next activity?

  • skkeb

    thank you great way to do it ^_^

  • chollie

    Well done @Ravi

  • bhaliya hitesh

    Unforthunetly Stop In Api 14 samsung

  • bliveinhack

    For those who are looking for API level < 10 can use IntentCompat.FLAG_ACTIVITY_NEW_TASK and IntentCompat.FLAG_ACTIVITY_CLEAR_TASK.

  • Nimmagadda Gowtham

    What to do if we want to remove only last activity from activity stack

  • Narayana Reddy Seelam

    In my app i have 5 Activities like login, reg1,reg2,reg3 and menu.after completion of the reg goto to menu activity.when i press the back button it will come to login but it come to reg3 activity.Please help me.Thank u

    • Hardik Kotadiya

      You can Use Above code for your problem solution.I think It can be work.You just need to clear top flags.

  • Prashant

    Owsm..thanks .Please provide solution for pre 11 version…

  • urvish91

    Hello Ravi, It’s perfectly running. but if i have given animation for transition for activities in style.xml. or by using overridependingtransition then animation is not working. can you please check and help me.?

  • Elyes Zliaa

    sorry but doesn’t work

  • Maulik Dadhaniya

    This is not work for me . When i click back button , it goto back activity but display blank.

  • Kevin Arturo Ramirez Zavalza

    Works great, thanks.

  • Sara Javed

    Hello Ravi , I had an issue with my login activity as I click on login button nothing happens even after 3 4 clicks but when i destroy the app from the device and then click on the app it opens the home page (which appears after login). What is the issue ? Where is my coding wrong ?

    • Sunny Dowari

      if you didn’t find the answer:

      you must check for your onClickListener and implement in correctly
      and its logging you in because there is this instance checking if you are a registered user.

      if (mAuth.getCurrentUser() != null) {

      //that means user is already logged in
      //so close this activity
      //and open your logged in activity
      startActivity(new Intent(getApplicationContext(), YourActivity.class));

      • Sara Javed

        Thank you !