22

我的活动导航和后台堆栈有问题,我希望你能帮我解决这个问题。

问题

根活动 >>> SecondActivity >> HomeButton

然后这会将我带到主页并从那里选择...

Gmail >> 消息 >> 在我的应用程序中打开附件 >> ImportActivity >> RootActivity

结果是启动了一个新任务,并使用了我的应用程序的另一个实例。这是不可接受的,因为我不想运行两个单独的任务,我只想要一个。

期望的结果

我想要发生的是当用户打开附件并且ImportActivity完成它所做的事情(它已android:noHistory设置为 true)并调用startActivity(intent),RootActivity启动时,但在原始任务中只保留一个实例和其余的活动(在这种情况下SecondActivity)被删除。

我想要这样做的原因是,如果用户在导入文件后导航出我的应用程序,然后触摸应用程序图标,它会加载第一个任务及其后堆栈,并且我有两个任务正在运行,用户可以在其中的两个不同部分我的申请。

我试过的

我玩过启动模式,但它们都没有真正给我我需要的功能。

我尝试过的启动模式..

android:launchMode="singleTask" - 每次启动时都会再次启动根活动。即使用户在我的应用程序中按下主页按钮并触摸应用程序图标,后堆栈也会被破坏。

android:launchMode="singleInstance = 不允许任何其他活动在任务中运行。

此外,在调用意图启动时,RootActivity我使用以下方法无济于事。

Intent i = new Intent(ImportActivity.this,TrackingActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

有可能做我想做的事吗?

提前致谢

4

3 回答 3

5

You say

Gmail >> Message >> Open attachment in my application >> ImportActivity >> RootActivity

but that may not be right. In this circumstance, gmail will issue an Intent targeted at the ImportActivity in your app. ImportActivity will execute. However, my reading of https://developer.android.com/guide/components/tasks-and-back-stack.html suggests that ImportActivity will execute as part of the same task as gmail and will be put on top of the back stack for the gmail task, unless you take special steps in the manifest to prevent that or gmail specifically invokes it as a separate task. When ImportActivity finishes, it shouldn't call startActivity(intentForRootActivity) but should just call finish() so that it will be destroyed and the activity from gmail which lies underneath it in the back stack will appear.

If ImportActivity did call startActivity(intentForRootActivity) then RootActivity would just go onto the top of the gmail task and appear on the gmail back stack. Touching home and then the launcher icon for gmail would see RootActivity reappear, hiding gmail underneath.

I think you need android:launchMode="standard" in the manifest declaration of ImportActivity.

The task which represents the older, stand-alone instance of your app will not be modified. If someone touches the launcher icon for your app, the old state of your app will be restored, unaffected by whatever happened in the gmail task.

The document http://developer.android.com/guide/components/processes-and-threads.html is rather vague about how Activities map onto processes here (causing David Wasser to comment on my earlier version of this answer) but it seems that the Activities of gmail will execute in one linux process with one linux user id and the ImportActivity will execute in another linux process with another user id. However, they can all form part of one task with one back stack.

ImportActivity will execute, as part of the gmail task, with the same effective Linux User ID as it would had it executed as part of your standalone app - and different from the Linux user ID that gmail executes with. This sounds unlikely and complicated but seems to be implied by https://developer.android.com/guide/components/fundamentals.html. That makes sense; if ImportActivity needs to get at, say, the user preferences stored for your app, it needs to read the preference file as if it were the user defined for your app, not the user defined for gmail.

I haven't tried executing any of this. If I have hold of entirely the wrong end of the stick, I'm sure someone will soon tell us!

于 2012-12-17T15:05:02.343 回答
1

你不应该需要任何特殊的东西launchMode来做到这一点。如果您的导入活动使用此启动根活动,Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP则应该可以执行您想要的操作。我猜你有问题taskAffinity。尝试这个:

在您的清单中,在 importActivity 的声明中添加以下内容:

android:taskAffinity=""

如果这不起作用,请发布您的清单,以便我们查看。

于 2012-12-17T16:48:01.737 回答
0

我真的不确定我是否明白了,但如果你想在不丢失额外应用程序堆栈的情况下进入你的“根”活动,它将Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP直接在你的标志上intent,并且没有额外的配置Manifest......如果你需要分离任务以创建一个新的完整的新堆栈,而不是Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP您描述的那样使用。

于 2012-12-12T19:46:41.447 回答