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

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

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

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

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

谢谢


当前回答

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

其他回答

试试这个:

intent.addFlags (Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)

它是API级别1,检查链接。

居然没有人提到这个优雅的解决方案,真是不可思议。这应该是公认的答案。

SplashActivity -> AuthActivity -> DashActivity

if (!sessionManager.isLoggedIn()) {
    Intent intent = new Intent(context, AuthActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    context.startActivity(intent);
    finish();
} else {
   Intent intent = new Intent(context, DashActivity.class);
   context.startActivity(intent);
    finish();
}

这里的关键是使用intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);为中介活动。一旦中间的链接断开,DashActivity将位于堆栈的第一个和最后一个。

android:noHistory="true"是一个糟糕的解决方案,因为它会在依赖Activity作为回调时引起问题,例如onActivityResult。这是推荐的解决方案,应该被接受。

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

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

我用这种方法。

Intent i = new Intent(MyOldActivity.this, MyNewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);