我的应用程序显示注册活动的第一次用户运行应用程序,看起来像:
ActivitySplashScreen(欢迎加入游戏,注册账号吗?)
ActivitySplashScreenSignUp(很好,填写这个信息)
ActivityGameMain(游戏主界面)
因此,当用户单击每个屏幕上的按钮时,这些活动以相同的顺序相互启动。
当用户从活动#2到#3时,是否有可能将#1和#2从历史堆栈中完全擦除?我希望这样,如果用户处于#3,并点击返回按钮,他们只会回到主屏幕,而不是回到启动画面。
我想我可以完成任务。开始第三项任务,但想看看是否有更简单的方法,
谢谢
这可能不是理想的方法。如果有人有更好的办法,我会很期待去实施。以下是我如何使用11版本前的sdk完成这一特定任务。
在每节课上,你都想在时间清楚的时候离开,你需要这样做:
... interesting code stuff ...
Intent i = new Intent(MyActivityThatNeedsToGo.this, NextActivity.class);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == R.string.unwind_stack_result_id) {
this.setResult(R.string.unwind_stack_result_id);
this.finish();
}
}
然后,当你想要初始化它时,需要从堆栈中设置弹出链的函数需要调用this:
NextActivity.this.setResult(R.string.unwind_stack_result_id);
NextActivity.this.finish();
那么活动就不在堆栈上了!
请记住,你可以启动一个活动,然后开始清理它的背后,执行并不遵循一个单一的(ui)线程。
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();
}
}