4

The problem is somewhat odd and after having trying to figure it out for about a day now, I am posting it here. I have an application where an activity A(main activity) launches other activities(B,C or D). The issue here happens when activity A has started Activity B and 'home' button is pressed.

Case 1 - When I test my application in debug mode on my device (HTC Desire) after pressing the 'home' button, I again click the application icon, it returns to the same activity (activity B), which is what is should do. No issues here.

Case 2 - When I export the signed package, and then install the application on the same device, then if I click the application icon after pressing the 'home' button, then a new instance of activity A (main activity) is launched ON TOP of activity B. I got to know this because when I press 'back' from that activity, it returns to activity B and pressing 'back' again shown activity A.

The behavior ceases to exist if the application is quit in the same order it was started, that is, if I press 'back' from activity B, then 'back' from activity A (exit).

After this everything runs fine. I have tested it many times with different settings but I can't seem to figure out why the behavior is like this.

Any help is appreciated.

4

4 回答 4

0

关于案例 1:从 Activity A 启动 Intent 以启动 Activity B 时,添加标志FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 这将确保当您回家并再次启动应用程序时,将显示 Activity A。

关于案例 2:我不确定这将如何发生。似乎它认为您有两个版本的应用程序,签名版本和未签名版本,但将它们都保存在同一个任务堆栈中。如果您只需要一个实例,您可能需要考虑为您的 Activity 使用 singleTask 或 singleInstance。有关更多详细信息,请参阅有关任务和回栈的文档。

于 2011-06-12T16:36:29.150 回答
0

I would agree with Noel regarding the likely cause of Case 2. Without task reparenting or it being set to a launchmode preventing multiple instances of an activity, there is a chance that launching it from Home isn't deemed the same stack as launching it from Eclipse (assuming this to be the case).

In my talent calculator app I have the whole application set allowTaskReparenting=true to ensure nothing is left in other stacks (primarily email as it can email launch urls). I then have my main activity set to launchMode="singleTask" as I only ever want one instance of this to exist no matter what launches it or with whatever intent.

My only other activity is for loading and saving and that has noHistory="true" to make sure it is removed and never returned to. That basically means it only exists while you're in it, and can never return to it.

clearTaskOnLaunch="true" will also ensure only the main Activity remains in the stack when it's launched from Home, but this isn't always the case if you have other ways to get into your activity. If it's only ever launched from Home then set this.

Hope that all helps.

于 2011-06-12T16:46:38.600 回答
0

我认为在清单中为活动 A 提供“单顶”标志应该可以解决此问题。

于 2011-06-12T16:36:00.883 回答
0

您是手动启动应用程序还是使用 Eclipse 或其他 IDE?从 Intellij IDEA 开始时,我遇到了完全相同的问题。然后我停下来手动运行它,行为符合预期。

于 2011-06-12T16:49:50.067 回答