Android的launchmode =" singleTask"不能按预期工作工作、launchmode、Android、singleTask

2023-09-06 01:27:59 作者:可惜故事太长只有风听我说

我有在后台运行,并显示经由通知系统的错误信息的应用程序。该通知有pendingIntent,导致后面的应用程序的主屏幕。在这个主屏幕上,我已经设置launchmode =singleTask。我的理解是从Android开发指南,这应该意味着,我的主要活动将永远只能有一个实例。

I have an application that runs in the background, and displays an error message via the notifications system. This notification has a pendingIntent that leads back the the app's main screen. On this main screen, I have set launchmode="singleTask". As I understand it from the Android Dev Guide, this should mean that my main activity will only ever have one instance.

但是,如果用户正在观看该活动的时候(或在应用程序内另一个),并进入和触摸通知以清除它,它会开始放活动的另一个副本栈上,因此,如果我打的后退按钮,它会再次返回到主屏幕(从主屏幕)。

However, if the user is viewing that activity at the time (or another one within the app), and goes and touches the notification to clear it, it goes ahead and puts another copy of the activity on the stack, so if I hit the back button, it will return to the main screen again (from the main screen).

为什么会做这个?

推荐答案

您几乎回答你自己的问题的问题;)

You are almost answering your own question in the question ;)

请尝试使用:

android:launchMode="singleInstance"

虽然被警告,如果你正在做anythign像startActivityForResult - !你将永远不会得到结果

Be warned though, if you are doing anythign like startActivityForResult - you will never receive the result!

更新:

如果你想获得新的使用onNewIntent意图数据:

If you want to receive new intent data using onNewIntent:

public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    setIntent(intent);
}

当您使用getIntent(),以将其传递给onNewIntent新意图将改变原意的应用程序返回。

That will change the intent the application returns when you use getIntent() to the new intent which was passed to onNewIntent.