我的应用程序有以下流程屏幕:

主屏->屏1->屏2->屏3->屏4->屏5

现在我在每个屏幕上都有一个公共注销按钮

(主屏/ 1屏/ 2屏/ 3屏/ 4屏/ 5屏)

我想当用户点击注销按钮(从任何屏幕),所有屏幕将完成,一个新的屏幕登录将打开。

我已经尝试了几乎所有的FLAG_ACTIVITY来实现这一点。 我还通过一些答案在stackoverflow,但不能解决这个问题。 我的应用程序是在Android 1.6上,所以不能使用FLAG_ACTIVITY_CLEAR_TASK

有什么办法解决这个问题吗?


当前回答

Use:

Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

这将清除所有的活动在顶部的家。

假设你完成了登录屏幕,当用户登录时,home被创建了,然后所有的屏幕从1到5在那个上面。我发布的代码将返回到主屏幕,完成所有其他活动。你可以在意图中添加额外的内容,并在主屏幕活动中读取并完成它(也许可以从那里再次启动登录屏幕或其他内容)。

我不确定,但你也可以尝试登录这个标志。我不知道在这种情况下活动将如何安排。所以不知道它是否会清除屏幕下面的那些,包括你现在所在的,但这绝对是可行的。

其他回答

我发现这个解决方案可以在每个设备上工作,尽管API级别(甚至小于11)

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

如果你的应用程序拥有最低sdk版本16,那么你可以使用finishAffinity()

完成此活动以及当前任务中紧接在其下面的具有相同关联的所有活动。

这是我的工作在顶部支付屏幕删除所有后台活动,

@Override
public void onBackPressed() {
         finishAffinity();
        startActivity(new Intent(PaymentDoneActivity.this,Home.class));
    } 

http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29

从developer.android.com:

public void finishAffinity () 在API级别16中添加

Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

请注意,此结束不允许您向前一个活动交付结果,如果您试图这样做,则会抛出异常。

我找到了这条路,它会清除所有历史并退出

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Intent intent = new Intent(getApplicationContext(), SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

finish();
System.exit(0);

在Kotlin中是这样的:

在另一个活动中(包含一些类),在Imports下面

var activity:Activity?=null
    get() = field
    set(value) {
        field = value
    }

然后,在onCreate下面

activity=this

现在在MainActivity中:

activity?.finish()