我的应用程序显示注册活动的第一次用户运行应用程序,看起来像:

ActivitySplashScreen(欢迎加入游戏,注册账号吗?) ActivitySplashScreenSignUp(很好,填写这个信息) ActivityGameMain(游戏主界面)

因此,当用户单击每个屏幕上的按钮时,这些活动以相同的顺序相互启动。

当用户从活动#2到#3时,是否有可能将#1和#2从历史堆栈中完全擦除?我希望这样,如果用户处于#3,并点击返回按钮,他们只会回到主屏幕,而不是回到启动画面。

我想我可以完成任务。开始第三项任务,但想看看是否有更简单的方法,

谢谢


当前回答

只需在startActivity(intent)之前调用this.finish(),就像这样-

       Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
        this.finish();
        startActivity(intent);

其他回答

太迟了,但希望能有所帮助。大多数答案都没有指向正确的方向。这种事情有两个简单的标志。

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

来自Android文档:

公共静态最终int FLAG_ACTIVITY_CLEAR_TASK 在API级别11中添加 如果在传递给Context.startActivity()的Intent中设置了该标志,该标志将导致任何与Context.startActivity()关联的现有任务 在启动活动之前要清除的活动。也就是 Activity将成为空任务的新根,以及任何旧任务的根 活动结束。这只能与 FLAG_ACTIVITY_NEW_TASK。

您可以使用转发从活动堆栈中删除前一个活动,同时启动下一个活动。APIDemos中有一个这样的例子,但基本上你所做的只是在调用startActivity()之后立即调用finish()。

I know I'm late on this (it's been two years since the question was asked) but I accomplished this by intercepting the back button press. Rather than checking for specific activities, I just look at the count and if it's less than 3 it simply sends the app to the back (pausing the app and returning the user to whatever was running before launch). I check for less than three because I only have one intro screen. Also, I check the count because my app allows the user to navigate back to the home screen through the menu, so this allows them to back up through other screens like normal if there are activities other than the intro screen on the stack.

//We want the home screen to behave like the bottom of the activity stack so we do not return to the initial screen
//unless the application has been killed. Users can toggle the session mode with a menu item at all other times.
@Override
public void onBackPressed() {
    //Check the activity stack and see if it's more than two deep (initial screen and home screen)
    //If it's more than two deep, then let the app proccess the press
    ActivityManager am = (ActivityManager)this.getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(3); //3 because we have to give it something. This is an arbitrary number
    int activityCount = tasks.get(0).numActivities;

    if (activityCount < 3)
    {
        moveTaskToBack(true);
    }
    else
    {
        super.onBackPressed();
    }
}

在这里我列出了一些完成这个任务的方法:

转到manifest.xml-并输入android:noHistory="true",从堆栈中删除活动。 当从当前活动切换到其他活动时,在意图设置标记为(意图。Intent.FLAG_ACTIVITY_CLEAR_TASK)。下面的示例演示了这一点。 Intent = new Intent(CurrentActivity. CurrentActivity.)这一点,HomeActivity.class); intent.setFlags(意图。FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) startActivity(意图);

注意:放置意图标志会导致一段时间的黑屏(当切换活动时)。

在API 11之前工作的一种方法是首先启动ActivityGameMain,然后在该活动的onCreate中启动ActivitySplashScreen活动。ActivityGameMain不会在你太早调用startActivity时出现。

然后你可以在启动ActivityGameMain时通过在Intent上设置这些标志来清除堆栈:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

您还必须将此添加到ActivitySplashScreen:

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

按回那个活动不会回到你的ActivityGameMain。

我假设你不希望启动画面回到任何一个,为了实现这一点,我建议将它设置为noHistory在你的AndroidManifest.xml。然后将goBackPressed代码放在ActivitySplashScreenSignUp类中。

然而,我发现了一些方法来打破这个。从通知中启动另一个应用程序,同时ActivitySplashScreenSignUp显示,并且不重置回溯历史。

唯一真正解决这个问题的方法是在API 11中:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);