应用程序始终从根活动重新启动,而不是恢复后台状态(已知错误)

发布于 2024-08-21 20:26:23 字数 1170 浏览 4 评论 0原文

我正面临这些链接中提到的问题:

http://code. google.com/p/android/issues/detail?id=2373

http://groups.google.com/group/android-developers/browse_thread/thread/77aedf6c7daea2ae/da073056831fd8f3?#da073056831fd8f3

http://groups.google.com/group/android-developers/browse_thread/thread/2d88391190be3303?tvc=2< /a>

我有一个简单的根活动,其中包含 LAUNCHER 和 MAIN 意图,没有其他。我开始另一项活动时,清单中没有任何标志或任何额外的内容。

我启动应用程序(根活动)并从那里开始第二个活动。按主页按钮后,任务将转到后台。再次启动应用程序时(从启动器或按住最近应用程序的主页按钮),它会在现有堆栈顶部启动根活动的新实例。

如果我按后退按钮,新的“根”活动将关闭,旧的第二个活动可见,这意味着它在同一任务中启动根活动,而不是将任务带到前台。

为了解决这个问题,我将根活动的启动模式设置为singleTask。现在,当我按下主屏幕并再次启动应用程序时,它会清除旧根任务上方的活动,并将旧根任务带到前台,而不是仅仅将整个旧任务和第二个活动放在前面。请注意,旧的根任务仍然保留其应用程序状态,这意味着它不是新实例,但更高的活动已被终止。

它甚至发生在从市场下载的其他应用程序上。手动安装方法对我来说没有任何作用,它仍然以相同的方式启动。

I am facing exactly the problem mentioned in these links:

http://code.google.com/p/android/issues/detail?id=2373

http://groups.google.com/group/android-developers/browse_thread/thread/77aedf6c7daea2ae/da073056831fd8f3?#da073056831fd8f3

http://groups.google.com/group/android-developers/browse_thread/thread/2d88391190be3303?tvc=2

I have a simple root activity with the LAUNCHER and MAIN intents and nothing else. I start another activity with has no flags or anything extra in the manifest whatsoever.

I launch the app (root activity) and from there start the 2nd activity. On pressing the Home button the task goes to the background. On launching the app again (from Launcher or from holding the Home button for recent apps) it starts a new instance of the root activity on top of the existing stack.

If I press the back button, the new "root" activity closes and the old 2nd activity is visible, which means its launching the root activity in the same task instead of bring the task to the foreground.

To counter this I made the root activity's launch Mode singleTask. Now when I press home and launch the app again, it clears the activities above the old root task and brings the old root task to the foreground instead of just bringing the entire old task with the 2nd activity on top to the front. Note that the old root task still retains its application state, which means it wasn't a new instance, but the the higher activities had been killed.

It even occurs on other applications downloaded from the market. The manual install method has no effect for me, it still launches the same way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隐诗 2024-08-28 20:26:23

这是因为用于启动应用程序的意图不同。 Eclipse 使用没有任何操作和类别的 Intent 启动应用程序。启动器使用 android.intent.action.MAIN 操作和 android.intent.category.LAUNCHER 类别的意图启动应用程序。安装程序会启动一个带有 android.intent.action.MAIN 操作且没有类别的应用程序。

无论是谁提交了这个 bug,都应该将其表述为增强 Eclipse 插件的请求,因为他们显然希望 Eclipse 能够假装是启动器,并使用与启动器相同的意图来启动应用程序。

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

Whoever submitted the bug should have probably worded it as a request for enhancement to the Eclipse plugin since they apparently want Eclipse to have the ability to pretend to be the launcher and to start apps using the same intent as the launcher.

2024-08-28 20:26:23

这是解决方案:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0 & getIntent().getExtras() == null) {
        finish();
        return;
    }

 Your code....
}

编辑:我在新意图和通知方面遇到问题。以前的解决方案不适用于通知和意图......

Here is the solution:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0 & getIntent().getExtras() == null) {
        finish();
        return;
    }

 Your code....
}

EDIT: I had problems with new intents and notifications. The previous solution doesn't work with notifications and intents...

寄人书 2024-08-28 20:26:23

只需将其添加到启动器活动的 onCreate 方法中,如下所示:

      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    if (!isTaskRoot()) {
        finish();
        return;
    }
    // other function
    }

Just add this in onCreate method of your launcher activity like this:

      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    if (!isTaskRoot()) {
        finish();
        return;
    }
    // other function
    }
万劫不复 2024-08-28 20:26:23

Xamarin.Android 的类似解决方案:

if (!IsTaskRoot)
            {
                string action = this.Intent.Action;
                if (this.Intent.HasCategory(Intent.CategoryLauncher) && !string.IsNullOrEmpty(this.Intent.Action) && action == Intent.ActionMain)
                {
                    Finish();
                    return;
                }
            }

Similar solution for Xamarin.Android:

if (!IsTaskRoot)
            {
                string action = this.Intent.Action;
                if (this.Intent.HasCategory(Intent.CategoryLauncher) && !string.IsNullOrEmpty(this.Intent.Action) && action == Intent.ActionMain)
                {
                    Finish();
                    return;
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文