android删除历史定位,android - 从历史记录中删除活动

android - 从历史记录中删除活动

我的应用在用户首次运行应用时显示了注册活动,如下所示:

活动启动画面(欢迎使用游戏,注册帐户?)

ActivitySplashScreenSignUp(很棒,请填写此信息)

ActivityGameMain(主游戏画面)

因此,当用户点击每个屏幕上的按钮时,活动将以完全相同的顺序相互启动。

当用户从活动#2转到#3时,是否可以完全从历史堆栈中删除#1和#2? 我喜欢它,以便如果用户在#3,并点击后退按钮,他们只是转到主屏幕,而不是回到启动画面。

我想我可以用任务来完成这个任务(即在#3上开始一个新任务)但是想看看是否有更简单的方法,

谢谢

14个解决方案

590 votes

您可以通过在AndroidManifest.xml文件中的相关条目中将"true"属性设置为"true"来实现此目的。 例如:

android:name=".AnyActivity"

android:noHistory="true" />

Aitor Gómez answered 2019-01-26T14:14:54Z

133 votes

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

Daniel Lew answered 2019-01-26T14:15:19Z

49 votes

是的,看一下Intent.FLAG_ACTIVITY_NO_HISTORY。

Matthias answered 2019-01-26T14:15:43Z

23 votes

这可能不是理想的做法。 如果某人有更好的方法,我将期待实施它。 以下是我使用pre-version-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();

}

}

然后需要从堆栈中引发弹出链的那个需要在你想要启动时调用它:

NextActivity.this.setResult(R.string.unwind_stack_result_id);

NextActivity.this.finish();

然后活动不在堆栈上!

记住,你可以开始一个活动,然后开始清理它,执行不遵循单个(ui)线程。

Travis answered 2019-01-26T14:17:05Z

20 votes

在API 11之前工作的一种方法是首先启动ActivitySplashScreenSignUp,然后在该Activity的AndroidManifest.xml中启动您的goBackPressed活动。 ActivitySplashScreenSignUp将不会出现,因为你很快就会为startActivity调用startActivity。

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

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

您还必须将其添加到ActivitySplashScreen:

@Override

public void onBackPressed() {

moveTaskToBack(true);

}

因此,按下该活动不会返回到您的ActivitySplashScreenSignUp。

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

但是我找到了一些方法来打破这个。 从通知启动另一个应用程序,同时显示ActivitySplashScreenSignUp并且不重置后退历史记录。

API 11中唯一真正的解决方法是:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

mxcl answered 2019-01-26T14:19:06Z

15 votes

我用这种方式。

Intent i = new Intent(MyOldActivity.this, MyNewActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)

startActivity(i);

Smiderle answered 2019-01-26T14:19:31Z

8 votes

我知道我迟到了(问题问题已经过去两年了)但是我通过拦截后退按钮完成了这个。 我只是查看计数,而不是检查特定活动,如果它小于3,它只是将应用程序发送到后面(暂停应用程序并将用户返回到启动前运行的任何内容)。 我检查的不到三个,因为我只有一个介绍屏幕。 此外,我检查计数,因为我的应用程序允许用户通过菜单导航回主屏幕,因此这允许他们通过其他屏幕备份,如果除了堆栈上的介绍屏幕之外的其他活动。

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

}

}

alphonzo79 answered 2019-01-26T14:19:56Z

6 votes

只需在Manifest文件中设置noHistory="true"即可。它使活动从后台堆栈中删除。

Stanislaw Brzezinski answered 2019-01-26T14:20:20Z

5 votes

在清单中,您可以添加:

android:noHistory="true"

android:name=".ActivityName"

android:noHistory="true" />

你也可以打电话

finish()

调用startActivity(..)后立即

Anubhav answered 2019-01-26T14:20:53Z

3 votes

对于API&gt; = 15到API 23其他解决方案不适用于我的情况。 最后我得到了这个。

Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);

nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);

startActivity(nextScreen);

ActivityCompat.finishAffinity(currentActivity.this);

lotus weaver answered 2019-01-26T14:21:15Z

3 votes

没人提到这个优雅的解决方案真是太疯狂了。 这应该是公认的答案。

android:noHistory="true"

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

}

这里的关键是使用android:noHistory="true"作为中介Activity.一旦中间链接断开,onActivityResult将成为堆栈中的第一个和最后一个。

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

Rowland Mtetezi answered 2019-01-26T14:22:12Z

2 votes

现在为时已晚,但希望它有所帮助。 大多数答案都没有指向正确的方向。 这样的事情有两个简单的标志。

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

来自Android文档:

public static final int FLAG_ACTIVITY_CLEAR_TASK       在API级别11中添加

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the

活动开始前要清除的活动。 那就是   活动成为一个空的任务的新根,以及任何旧的任务   活动结束了。 这只能与。一起使用  FLAG_ACTIVITY_NEW_TASK。

Nouman Ghaffar answered 2019-01-26T14:23:46Z

1 votes

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

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

this.finish();

startActivity(intent);

answered 2019-01-26T14:24:09Z

-7 votes

试试这个:

intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)

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

Alécio Carvalho answered 2019-01-26T14:24:53Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值