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

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

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

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

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

谢谢


当前回答

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.finishAndRemoveTask();
        }
        else {
            super.finish();
        }

其他回答

从历史记录中删除一个活动是通过在你不想要的活动之前设置标志来完成的

A - C > B - > - > D

假设A,B,C和D是4个活动,如果你想清除B和C 设置标志

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

在活动A和B中

这是代码位

Intent intent = new Intent(this,Activity_B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

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

只需在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。

在清单中,您可以添加:

android:noHistory="true"

<activity
android:name=".ActivityName"
android:noHistory="true" />

你也可以打电话

finish()

在调用startActivity(..)