Android:打开活动而不保存到堆栈中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12358485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:27:47  来源:igfitidea点击:

Android: open activity without save into the stack

androidback-stack

提问by realtebo

I have 2 activities: Main and List.

我有 2 个活动:主要和列表。

From Main you can open List; from List you can open Main.

从 Main 您可以打开 List;从列表中,您可以打开 Main。

I'd like it so that every opening of List does notget saved into the 'history'. So, pressing back from Main cannotreturn to List.

我想它,以便名单的每一个开放并没有得到保存到“历史”。因此,从 Main返回无法返回 List。

Is it possible?

是否可以?

回答by Eric

When starting your list's Activity, set its Intentflags like so:

启动列表时Activity,请设置其Intent标志,如下所示:

Intent i = new Intent(...); // Your list's Intent
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag
startActivity(i);

The FLAG_ACTIVITY_NO_HISTORYflag keeps the new Activityfrom being added to the history stack.

FLAG_ACTIVITY_NO_HISTORY标志可防止将新Activity添加到历史堆栈中。

NB: As @Sam points out, you can use i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);instead. There is no functional difference.

注意:正如@Sam 指出的那样,您可以i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);改用。没有功能差异。

回答by Marcin S.

In the manifest file add:

在清单文件中添加:

android:noHistory="true" 

to the activity that you don't want to keep on the stack.

到您不想保留在堆栈中的活动。

回答by Kalel Wade

Use the new task with clearing. This worked in my case when the other options didnt.

使用新任务进行清算。当其他选项没有时,这在我的情况下有效。

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

Clear the entire history stack and start a new activity on Android

清除整个历史堆栈并在 Android 上启动一个新活动

回答by dac2009

It seems, if you call finish() on your activity right after you have opened another, the one that is finished is removed from the stack.

看起来,如果您在打开另一个活动后立即对您的活动调用 finish() ,则已完成的活动将从堆栈中删除。

for example:

例如:

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

回答by Fred

In my particular case FLAG_ACTIVITY_NO_HISTORYdid not work. Neither did FLAG_ACTIVITY_NEW_TASKor FLAG_ACTIVITY_CLEAR_TASKalone by themselves work.

在我的特殊情况下FLAG_ACTIVITY_NO_HISTORY不起作用。没有FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK独自工作。

However FLAG_ACTIVITY_NEW_TASKand FLAG_ACTIVITY_CLEAR_TASKtogether did work.

然而FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK一起工作。

Intent intent = new Intent(FooActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

回答by Devin B

Just wanted to add a way to do this in Kotlin:

只是想在 Kotlin 中添加一种方法来做到这一点:

val i = Intent(this, LogInActivity::class.java)
startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK))

回答by Miku

Late answer, but adds some depth to other answers. It all comes down to what do you want to happen with other activities started from that activity

迟到的答案,但为其他答案增加了一些深度。这一切都归结为您希望从该活动开始的其他活动发生什么

Option 1 - Just this one activity should not have history of calling activity

选项 1 - 仅此一项活动不应具有调用活动的历史记录

Then just do:

然后就这样做:

Intent i = new Intent(...);
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);

Option 2 - All activities started from that specific activity should not have history

选项 2 - 从该特定活动开始的所有活动不应有历史记录

Then add in manifest of the calling activity:

然后添加调用活动的清单:

android:noHistory="true" 

But if you do want to have history in new activity, then you have to manually remove the flag:

但是,如果您确实希望在新活动中拥有历史记录,则必须手动删除该标志:

Intent i = new Intent(...);
i.removeFlag(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);

Hope that clears up other answers :)

希望能澄清其他答案:)

回答by live-love

Try FLAG_ACTIVITY_CLEAR_TOP if the activity is already running:

如果活动已经在运行,请尝试 FLAG_ACTIVITY_CLEAR_TOP:

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

回答by Broak

Can you not override the back button on the particular activity to stop the 'return' functionality?

您不能覆盖特定活动上的后退按钮以停止“返回”功能吗?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        return true;
    }
    return super.onKeyDown(keyCode, event);
}