简体   繁体   English

在不使用android:launchMode =“ singleTask”的情况下在新任务中打开android活动

[英]open android activity in new task without using android:launchMode=“singleTask”

I've created a browser application with main activity which response to the following intents: 我创建了一个浏览器应用程序,该应用程序具有主要活动,可以响应以下意图:

 <intent-filter> 
       <data android:scheme="http"/>
       <data android:scheme="https"/>
       <category android:name="android.intent.category.DEFAULT"/>
       <category android:name="android.intent.category.BROWSABLE"/>
       <action android:name="android.intent.action.VIEW"/>
 </intent-filter>

On url click from other task (gmail, sms) if i choose my application, the activity is open in the same task as the calling task. 在url上,如果我选择我的应用程序,则从其他任务(gmail,sms)单击,该活动在与调用任务相同的任务中打开。 When I choose different browser (Mozila firefox, chrome, dolphine) they are opening in different task. 当我选择其他浏览器(Mozila firefox,chrome,dolphine)时,它们将打开不同的任务。

Looking on other browsers manifest, I see that no one using android:launchMode="singleTask" . 查看其他浏览器清单,发现没有人使用android:launchMode =“ singleTask”

I don't want to use single task flag since it is not recommended by google and also makes me other prolems. 我不想使用单个任务标志,因为Google不推荐使用它,这也给我带来了其他问题。

I tried to understand how does other browsers do it but didn't figure it out. 我试图了解其他浏览器是如何做到的,但没有弄清楚。

any ideas? 有任何想法吗? is there other way to open my activity in different task without using singleTask flag? 还有其他方法可以在不使用singleTask标志的情况下在其他任务中打开我的活动吗?

You can check in onCreate if the activity is running in a single task. 如果活动在单个任务中运行,则可以签入onCreate。 If not just finish() it and create it again with using FLAG_ACTIVITY_NEW_TASK 如果不是,请完成()并使用FLAG_ACTIVITY_NEW_TASK重新创建

This may help you 这可能对您有帮助

   ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (RunningTaskInfo task : tasks) {
        if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName())) 
          // check if it's the only one activity or whatever                               
    }

I have found the following description on Android Developers site : 我在Android Developers 网站上找到了以下说明:

As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the element. 作为另一个示例,Android浏览器应用程序通过在元素中指定singleTask启动模式来声明Web浏览器活动应始终在其自己的任务中打开。 This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. 这意味着,如果您的应用程序发出打开Android浏览器的意图,则其活动不会与您的应用程序放在同一任务中。 Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent. 取而代之的是,要么为浏览器启动一个新任务,要么,如果浏览器已经在后台运行了一个任务,则将该任务带到新的位置以处理新的意图。

As you can see android:launchMode=singleTask is the right choice in your case. 如您所见, android:launchMode=singleTask是您情况下的正确选择。 You already mentioned that you had issues with this property so maybe let's focus on them. 您已经提到过此属性存在问题,所以也许让我们关注它们。

Update 28.05.2014 更新28.05.2014

Note from Google regarding singleTask launchMode: Google关于singleTask launchMode的说明:

The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications. 其他模式(singleTask和singleInstance)不适用于大多数应用程序,因为它们导致的交互模型可能对用户来说是陌生的,并且与大多数其他应用程序有很大不同。

singleTask and singleInstance use case from Google: Google的singleTask和singleInstance用例:

Specialized launches (not recommended for general use) 专门发射(不建议一般使用)

As you can see singleTask may not be recommended for general use but your case is not general, actually it's one of the cases where singleTask fits perfectly. 如您所见,可能不建议将singleTask用于一般用途,但您的情况不是一般情况,实际上,这是singleTask非常适合的情况之一。 In other words, singleTask is not forbidden, it's just need to be used with caution in order to provide end users common experience with your app. 换句话说,singleTask是不被禁止的,只需要谨慎使用它,以便为最终用户提供有关您的应用程序的常见体验。

I hope I made it clear now for you. 希望我现在为您澄清。 Feel comfortable with this launch mode in your case. 在您的情况下,对这种启动模式感到满意。

You're trying to "bend the rules" a little, and you're not clear enough on what you're trying to avoid by not using android:launchMode="singleTask" . 您正在尝试“弯曲规则”,并且对于不尝试通过避免使用android:launchMode="singleTask"避免的事情还不够清楚。

So I would suggest researching into either: 因此,我建议研究以下任一方面:

  1. Creating a Service and having that Service listen to the intent filter. 创建一个Service并使该Service侦听意图过滤器。 Then having this Service open your Activity , and having the Activity 's affinity set correctly to match the Service 's. 然后让此Service打开您的Activity ,并正确设置Activity的相似性以匹配Service的亲和力。 This would allow you to get over the issue where affinity is not binding the Activity correctly. 这将使您克服亲和力未正确绑定Activity的问题。

  2. Having a silent Activity starting a new Activity and quiting. 让静默Activity开始一个新的Activity并退出。 The silent Activity would start in a new stack (not in a singletask mode), and would shut itself down upon starting the Activity you actually need. 静默Activity将在新堆栈中启动(而不是在单任务模式下),并且在启动您实际需要的Activity时会自行关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM