活动结束之前发表的所有活动结束

由网友(别紧张,我不是好人)分享简介:我要完成所有这一切都在想办法从堆栈中删除所有的家长活动运行的应用程序的活动。I want to finish all the activities which are running in the application means want to remove all the parent activities f...

我要完成所有这一切都在想办法从堆栈中删除所有的家长活动运行的应用程序的活动。

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

推荐答案

我应该让你知道这是不是在Android的推荐行为,因为你应该让自己来管理活动的生活圈子。

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

不过,如果你真的需要做到这一点,你可以使用FLAG_ACTIVITY_CLEAR_TOP

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

我给你一些示例code在这里,在这里MainActivity是在应用程序中的第一个活动

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);
    }
}

如果你想退出整个应用程序,您可以使用下面的code和检查的MainActivity完全退出该应用程序:

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.

阅读全文

相关推荐

最新文章