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
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);
Author

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

      Its Intent.FLAG_ACTIVITY_CLEAR_TOP

  • 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

    Hi
    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.?