java 替代 Intent.FLAG_ACTIVITY_CLEAR_TASK

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11098279/
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-10-31 03:46:45  来源:igfitidea点击:

Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK

javaandroidandroid-intent

提问by Jason Smith

I have two apps App-B launches App-A. If the user starts App B from inside App A I call finish on App-A so I have no problem.

我有两个应用程序 App-B 启动 App-A。如果用户从 App AI 内部启动 App B,则在 App-A 上调用完成,所以我没有问题。

If the user goes straight to App B from the Application drawer or long pressing home button then I implement the below which clears the task in App A first before applying all the extras. This has the desired affect but only works on API 11. On lower APIs the new task in APP-A will not change and extras putExtra will have no effect. Any alternative to FLAG_ACTIVITY_CLEAR_TASK? for API <=10?

如果用户从应用程序抽屉或长按主页按钮直接进入应用程序 B,那么我将执行以下操作,在应用所有附加功能之前首先清除应用程序 A 中的任务。这具有预期的效果,但仅适用于 API 11。在较低的 API 上,APP-A 中的新任务不会改变,额外的 putExtra 将不起作用。有FLAG_ACTIVITY_CLEAR_TASK什么替代品吗?对于 API <=10?

        Intent i = new Intent("com.App-A");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Thanks

谢谢

Jason

杰森

回答by Warpzit

The new IntentCompatshould've helped on that, but apparently the flag is ignored for API lower than 11.

新的IntentCompat应该对此有所帮助,但显然低于 11 的 API 忽略了该标志。

To use IntentCompat do following:

要使用 IntentCompat,请执行以下操作:

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

回答by sumit

this will work correctly

这将正常工作

i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY | 
               Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

回答by Matt Clark

I may be wrong in understanding what you are asking, but is it that when you launch B, you want A to be killed?

我可能理解你在问什么是错误的,但是当你启动 B 时,你是否希望 A 被杀死?

In A, add this to the activity tag in the manifest:

在 A 中,将此添加到清单中的活动标签:

android:noHistory=true

This will cause the activity to be removed from memory as soon as it loses focus.

这将导致活动在失去焦点后立即从内存中删除。

回答by Tom Mulcahy

The best documentation I've found for these Intent flags is here: http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

我为这些 Intent 标志找到的最好的文档在这里:http: //blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

I don't understand what you are trying to do, but have you tried FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET?

我不明白你想做什么,但你试过FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET吗?

回答by Jason Hessley

Using FLAG_ACTIVITY_CLEAR_TASKclears the back stack. If I'm understanding correctly, this is the behavior you want.

使用FLAG_ACTIVITY_CLEAR_TASK清除返回堆栈。如果我理解正确,这就是您想要的行为。

Using singleInstanceinstead of singleTaskin your manifest will do this.

在您的清单中使用singleInstance而不是singleTask这样做。

In the comments you said that it must be singleTask. I'm assuming this is because you need the back stack in certain circumstances.

在评论中你说它必须是singleTask。我假设这是因为在某些情况下您需要后台堆栈。

Since launchModecan't be changed programaticaly and FLAG_ACTIVITY_CLEAR_TASKis not availble for API <=10, you may have to create two identical activities.

由于launchMode无法以编程方式更改并且FLAG_ACTIVITY_CLEAR_TASK不适用于 API <=10,因此您可能必须创建两个相同的活动。

One with launchMode=singleTaskand one with launchMode=singleInstance.

一个launchMode=singleTask和一个launchMode=singleInstance

Add this to the one using singleInstanceto get a clear stack when launched from the app drawer:

将此添加到singleInstance用于从应用程序抽屉启动时获得清晰堆栈的一个:

 <category android:name="android.intent.category.LAUNCHER" />

回答by David Wasser

I'm still having a lot of trouble understanding the problem but would like to help you fix it. Since comments only allow 600 characters and don't format well, I'm going to create an answer instead, because I'm sure that together we can get this resolved.

我在理解问题时仍然遇到很多麻烦,但我想帮助您解决问题。由于评论只允许 600 个字符并且格式不正确,我将创建一个答案,因为我相信我们可以一起解决这个问题。

I would like to be able to reproduce your problem. To do that I've created 2 applications: AppAand AppB. AppA has a single activity called ActivityAand AppB has a single activity called ActivityB. Both ActivityAand ActivityBuse android:launchMode="singleTask".

我希望能够重现您的问题。为此,我创建了 2 个应用程序:AppAAppB。AppA 有一个名为 的活动,ActivityA而 AppB 有一个名为 的活动ActivityB。无论ActivityAActivityB使用android:launchMode="singleTask"

ActivityAhas a button on it that launches AppB and finishes, like this:

ActivityA上面有一个按钮,可以启动 AppB 并完成,如下所示:

    Intent intent = new Intent("de.sharpmind.example.AppB");
    intent.putExtra("extra", "launched from AppA");
    startActivity(intent);
    finish();

ActivityBhas a button on it that launches AppA like this:

ActivityB上面有一个按钮,可以像这样启动 AppA:

    Intent intent = new Intent("de.sharpmind.example.AppA");
    intent.putExtra("extra", "launched from AppB");
    startActivity(intent);

This all works as I expect it to. AppA and AppB run in different tasks. The "extra" is properly seen in the onCreate()methods of each application.

这一切都按我的预期工作。AppA 和 AppB 在不同的任务中运行。在onCreate()每个应用程序的方法中正确地看到了“额外” 。

So, can you please tell me more about your problem. How can I reproduce exactly your problem? You wrote:

所以,你能告诉我更多关于你的问题吗?我怎样才能准确地重现你的问题?你写了:

On lower APIs the new task in APP-A will not change and extras putExtra will have no effect.

在较低的 API 上,APP-A 中的新任务不会改变,额外的 putExtra 将不起作用。

What do you mean by that? Where are you putting extras and where are you getting them and what do you expect to happen?

你是什​​么意思?你把额外的东西放在哪里,你从哪里得到它们,你预计会发生什么?

Also, what is the launchModeof your AppB?

另外,launchMode你的AppB?

Also, when you have this problem, are there other activities in the task stack for AppA?

另外,当您遇到此问题时,AppA 的任务堆栈中是否还有其他活动?

Please provide more info, either in your original question or here as comments.

请在您的原始问题中或在此处作为评论提供更多信息。