Android 应用程序总是从根活动开始,而不是恢复后台状态(已知错误)

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

App always starts fresh from root activity instead of resuming background state (Known Bug)

androidandroid-activityresumetask

提问by Monstieur

I am facing exactly the problem mentioned in these links:

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

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

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/77aedf6c7daea2ae/da073056831fd8f3?#da073056831fd8f3

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

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 extrain the manifest whatsoever.

我有一个带有 LAUNCHER 和 MAIN 意图的简单根活动,没有别的。我开始另一个活动,但清单中没有任何标志或任何额外东西

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.

我启动应用程序(根活动)并从那里开始第二个活动。按下主页按钮后,任务进入后台。再次启动应用程序时(从 Launcher 或通过按住 Home 按钮查看最近的应用程序),它会在现有堆栈顶部启动根活动的新实例。

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.

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

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.

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

采纳答案by Lance Nanek

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.

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

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.

提交该错误的人可能应该将其表述为对 Eclipse 插件的增强请求,因为他们显然希望 Eclipse 能够伪装成启动器并使用与启动器相同的意图启动应用程序。

回答by Sachin Gurnani

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
        // Activity was brought to front and not created, 
        // Thus finishing this will get us to the last viewed activity 
        finish(); 
        return; 
    } 

    // Regular activity creation code... 
} 

回答by toni

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...

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

回答by Amal Kronz

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

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

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

回答by CDrosos

Similar solution for Xamarin.Android:

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;
                }
            }