问题描述
我想完成应用程序中正在运行的所有活动意味着想从堆栈中删除所有父活动.
我想在我的应用程序中本地实现注销功能,所以我的想法是,我将完成之前开始的所有活动,然后再次开始登录活动..
我应该让你知道这不是 android 推荐的行为,因为你应该让自己管理活动的生命周期.
但是如果你真的需要这样做,你可以使用 FLAG_ACTIVITY_CLEAR_TOPp>
我在这里给你一些示例代码,其中 MainActivity 是应用程序中的第一个活动:
public static void home(Context ctx) {if (!(ctx instanceof MainMenuActivity)) {Intent 意图 = new Intent(ctx, MainMenuActivity.class);意图.setFlags(意图.FLAG_ACTIVITY_CLEAR_TOP);ctx.startActivity(intent);}}
如果您想退出整个应用程序,可以使用以下代码并在MainActivity中签入以完全退出应用程序:
public static void clearAndExit(Context ctx) {if (!(ctx instanceof MainMenuActivity)) {Intent 意图 = new Intent(ctx, MainMenuActivity.class);意图.setFlags(意图.FLAG_ACTIVITY_CLEAR_TOP);捆绑捆绑 = 新捆绑();bundle.putBoolean("退出", true);意图.putExtras(捆绑);ctx.startActivity(intent);} 别的 {((活动) ctx).finish();}}
希望这会有所帮助.
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..
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.
这篇关于完成活动前开始的所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!