Finishing all activities started before the activity

前端 未结 3 1540
难免孤独
难免孤独 2021-02-09 15:16

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 functional

相关标签:
3条回答
  • 2021-02-09 16:01

    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);
    
    0 讨论(0)
  • 2021-02-09 16:12

    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.

    0 讨论(0)
  • 2021-02-09 16:13

    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.

    0 讨论(0)
提交回复
热议问题