有一个实例活动有一个、实例

2023-09-12 04:56:21 作者:哥丿就是传说中的土豪

我有一个活动的导航问题,后面的堆栈,我希望你可以把它清除掉我。

I have a problem with Activity navigation and the back stack and I was hoping you could clear it up for me.

问题

根系活力>>> SecondActivity >>为homeButton

Root Activity >>> SecondActivity >> HomeButton

这然后把我的主页,并从那里我选择...

This then takes me to the home page and from there I select...

的Gmail >>在线留言>>打开附件在我的应用>> ImportActivity >> RootActivity

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

的结果是使用的一种新的任务被启动和我的应用的另一个实例。这是不可接受的,因为我不想要两个独立的任务运行的我只想要一个。

The outcome is a new task being started and another instance of my application being used. This is unacceptable as I don't want two separate tasks running I only want one.

期望的结果

当用户打开附件和 ImportActivity 完成什么它做什么(它有安卓我希望发生的是),并调用 startActivity(意向) RootActivity 已启动,但只保留一个实例和活动上休息它的上面,在原始任务(在此情况 SecondActivity )被除去。

What I want to happen is when the user opens the attachment and ImportActivity finishes what its doing (it has android:noHistory set to true) and calls startActivity(intent), RootActivity is started, but keeping only one instance and the rest of the activities on top of it in the original task (in this case SecondActivity)are removed.

我想这样做的原因是,如果用户浏览了我的应用程序导入文件后,再触摸它加载的第一个任务,其背部栈的应用图标,然后我在运行中,用户可以在两个两个任务我的应用程序的不同部分一次。

The reason I want this is if the user navigates out of my application after importing the file and then touches the app icon it loads the first task with its back stack and I have two tasks running in which the user can be in two different parts of my application at once.

我已经试过

我打周围的发射模式,但没有一个是给我的,我需要的功能。

I have played around with the launch mode but none of them really give me the functionality that I need.

启动模式我都试过。

安卓launchMode =singleTask的 - 这刚开始有人推出了根系活力又各一次。即使用户pressed home键我的应用程序中,碰到的应用程序图标后面堆被摧毁。

android:launchMode="singleTask" - this just started the root activity again each time it was launched. Even if the user pressed the home button inside my application and touched the App icon the back stack was destroyed.

机器人:launchMode =singleInstance 的=不允许任何其他activites在任务执行

android:launchMode="singleInstance = doesn't allow any other activites to run in the task.

另外调用的意图时,启动 RootActivity 我使用了下列无济于事。

Also when calling the intent to start the RootActivity I have used the following to no avail.

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

是否有可能做我想做的?

Is it possible to do what I want?

在此先感谢

推荐答案

您说

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

但可能是不对的。在这种情况下,Gmail会问题,在您的应用程序针对的是 ImportActivity 意图 ImportActivity 将执行。但是,我读https://developer.android.com/guide/components/tasks-and-back-stack.html建议 ImportActivity 将作为同一任务的Gmail的一部分,除非你在清单中,以特步执行,将放在后面堆栈gmail的任务之上, prevent的或Gmail专门调用它作为一个单独的任务。当 ImportActivity 结束,它不应该叫 startActivity(intentForRootActivity),但应该只是叫完成(),这样它会被销毁,从Gmail它位于其下方的背部栈的活动将出现。

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.

如果 ImportActivity 打过电话 startActivity(intentForRootActivity)然后 RootActivity 只想去到Gmail的任务的顶部,并出现在Gmail中回堆栈。触摸家居,然后使用Gmail的启动器图标将看到 RootActivity 再次出现,隐藏的Gmail下方。

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.

我想你需要机器人:launchMode =标准 ImportActivity 的舱单申报

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

这将重新$ P $任务psents您的应用程序的旧的,独立实例不会被修改。如果有人触动了您的应用程序启动器图标,您的应用程序的老态会恢复,不会影响在Gmail的任务,不管发生什么事情。

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.

文件http://developer.android.com/guide/components/processes-and-threads.html是相当含糊的活动如何在这里映射到进程(造成大卫·瓦瑟对这个回答我的早期版本的评论),但似乎Gmail中的活动将在一个Linux进程有一个Linux用户ID和 ImportActivity 将在另一个linux的过程中与其他用户ID执行。但是,它们都可以形成一个任务与一回堆栈的一部分。

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 将执行,因为Gmail的任务的一部分,同样有效的Linux用户ID,因为这将有它执行为您的独立应用程序的一部分 - 从不同这Gmail中的Linux用户ID与执行。这听起来不太复杂,但似乎由https://developer.android.com/guide/components/fundamentals.html.这就说得通了;如果 ImportActivity 需要得到的,比如说,用户preferences保存您的应用程序,它需要读取preference文件,就好像它是用户定义为您的应用程序,而不是用户定义的gmail。

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!