Finishing all activities started before the activity

10,812

Solution 1

I should let you know this is not a recommended behavior in android since you should let itself to manage life circles of activities.

However if you really need to do this, you can use FLAG_ACTIVITY_CLEAR_TOP

I give you some sample code here, where MainActivity is the first activity in the application:

public static void home(Context ctx) {
    if (!(ctx instanceof MainMenuActivity)) {
        Intent intent = new Intent(ctx, MainMenuActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ctx.startActivity(intent);
    }
}

If you want to quit whole application, you can use the following code and check in the MainActivity to quit the application completely:

    public static void clearAndExit(Context ctx) {
    if (!(ctx instanceof MainMenuActivity)) {
        Intent intent = new Intent(ctx, MainMenuActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        bundle.putBoolean("exit", true);
        intent.putExtras(bundle);
        ctx.startActivity(intent);
    } else {
        ((Activity) ctx).finish();
    }
}

Hope this helps.

Solution 2

Try this one if you're targetting APi Level <11

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

Solution 3

What you might be looking for is FLAG_ACTIVITY_CLEAR_TOP intent flag:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

Intent i = new Intent(..);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

and then start Activity with this intent.

See more on tasks and back stack in documentation: Tasks and Back stack.

However to implement login/logout to application (if it is not communicating with online service) you might use SharedPreferences. This way on application start you could check if user is logged in (eg. some flag turned on in preferences) and upon application exit (eg. by button) you could clear this flag.

Killing/removing Activities should be left to the system. Per system design it is Android OS that is responsible for application lifetime.

For example check the Password Safe application sources. It needs password every time you open fresh instance of application.

Share:
10,812
Pooja M. Bohora
Author by

Pooja M. Bohora

Develops applications for android and iOS.

Updated on July 12, 2022

Comments

  • Pooja M. Bohora
    Pooja M. Bohora almost 2 years

    I want to finish all the activities which are running in the application means want to remove all the parent activities from stack.

    I want to implement logout functionality locally in my application so what I was thinking, I will finish all the activities started before and will start login activity again..

  • Pooja M. Bohora
    Pooja M. Bohora almost 13 years
    Hi Gil,I tried this but this do not work it show me all the activities which were called before. I am missing anything else?
  • Marcin Gil
    Marcin Gil almost 13 years
    So actually I find topic and explanation a bit confusing: in topic you want to finish everything started before starting new activity, while explanation suggests killing your application. Can you elaborate?
  • Pooja M. Bohora
    Pooja M. Bohora almost 13 years
    no success yet.. any more suggestions? actually i want to implement logout functionality in my application..
  • ThinkChris
    ThinkChris almost 13 years
    Hi Pooja, I used the first block of codes to return to my first activity in my project. I think you can use it to return to login screen too?
  • Pooja M. Bohora
    Pooja M. Bohora almost 13 years
    yes I understand but when I do so, on back key of device it shows the last opened activities.
  • Pooja M. Bohora
    Pooja M. Bohora almost 13 years
    Ohh... I got the problem. actually I was finishing the login activity once user login to application successfully. so the code you given was not working. but I have to finish login activity after successful login. how to achieve this? please help.
  • ThinkChris
    ThinkChris almost 13 years
    @Pooja You can try to use the second block of codes, you can put bundle.putBoolean("logout", true), then check bundle in onResume() of MainMenuActivity. If the bundle == "logout", then finish MainMenuAcitvity and launch LoginActivity...I don't think it is a clever approach, give me sometime after work and I will figure out a better way. Hope this helps:)
  • Pooja M. Bohora
    Pooja M. Bohora almost 13 years
    hello, but i am not getting use of bundle in this. will you please tell me about this?
  • Gem
    Gem over 9 years
    This is the best answer and we should rate this one.