使用Intent.FLAG_ACTIVITY_MULTIPLE_TASK是否安全? [英] Is it safe to use Intent.FLAG_ACTIVITY_MULTIPLE_TASK?

查看:1011
本文介绍了使用Intent.FLAG_ACTIVITY_MULTIPLE_TASK是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

让我们来看看下一个场景:

  1. 用户使用的应用X具有多个活动(例如gmail应用).
  2. 在浏览了应用X后,他进入了您的应用.
  3. 在您的应用中,您需要使用意图启动该X应用,然后转到其中的特定活动.
  4. 现在用户转到应用X上的特定活动.
  5. 用户按下后退"按钮,希望返回到您的应用,而不是停留在应用X上.

另一种类似的情况:

  1. 用户在活动之间浏览您的应用.
  2. 您的应用进入后台(例如,使用主页按钮).
  3. 您的应用程序显示一条通知,一旦单击该通知,它将打开您的应用程序的特定活动.
  4. 用户点击通知,然后转到您应用的特定活动.
  5. 用户按下后退"按钮,希望返回到单击通知之前显示的应用,而不是转到上一次离开应用时显示的上一个活动.

这两种情况下的最后一步似乎都不是默认行为.

这就是为什么我为此目的搜索标志的最佳组合.

问题

似乎可以实现我描述的行为的唯一标志是 Intent.FLAG_ACTIVITY_CLEAR_TASK 可以达到相似的结果,但是它们的行为怪异和/或使用了较高的API

问题

使用此标志是否安全?有什么好的替代方法吗?

使用此标志有什么危险,使用它的API上的描述是什么意思?

解决方案

在第一种情况下,如果您的应用需要启动另一个应用的活动,则可以在与您的应用相同的任务中启动该活动.无需为此使用任何特殊的Intent标志(不需要FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_MULTIPLE_TASK).在场景的第5步中,用户将返回到您的应用程序(因为BACK只是将他带到当前任务中的上一个活动).这是标准的默认行为.

在第二种情况下,我的第一个回答是这不是标准行为".这意味着用户可能不会期望能够在点击通知之前先返回到他们正在执行的任务.但是,如果您确实要实现此功能,那么我建议您创建一个从通知启动的特殊活动,并且该特殊活动应与应用程序中的其余活动具有不同的taskAffinity.在这种情况下,从通知启动特殊活动时,它将不将应用程序的任务置于前台.它将仅创建一个仅包含特殊活动的新任务.当显示特殊活动时,用户可以按BACK键,这将使他返回到正在执行的任务,然后单击您的通知.

通常,您不应该使用FLAG_ACTIVITY_MULTIPLE_TASK.主要原因是,如果您有多个包含应用程序(或应用程序的一部分)的任务,则用户几乎不可能返回到特定任务.无法提供不同的启动图标或不同的应用程序名称(针对不同的任务)(这样,用户将在最近的任务"列表中看到多个任务,但无法分辨出哪个是哪个.)将很难清理您正在做的事情,并且您将遇到的问题超出了您的处理范围.使用此标志大约有一百万种副作用,对于一般的应用程序,则不需要这样做.

Background

Let's take the next sceneraio:

  1. the user uses an app X which has multiple activities (like the gmail app).
  2. after navigating the app X a bit, he goes to your app.
  3. in your app, you need to start this X app using an intent, to go to a specific activity within it.
  4. now the user goes to this specific activity on the app X.
  5. the user presses the back button, hoping to go back to your app instead of staying on the app X.

another similar scenario:

  1. the user navigates on your app between activities.
  2. your app went to the background (using the home button, for example).
  3. your app shows a notification that once clicked, it will open a specific activity of your app.
  4. the user clicks on the notification and goes to the specific activity of your app.
  5. the user presses the back button, hoping to return to the app that was shown before clicking on the notification, instead of going to the previous activity on your app that was shown the last time it left it.

It seems that the last step on both scenarios isn't the default behavior.

This is why I've searched what is the best combination of flags for this purpose.

The problem

It seems that the only flag that can achieve the behavior i've described is Intent.FLAG_ACTIVITY_MULTIPLE_TASK (together with Intent.FLAG_ACTIVITY_NEW_TASK).

According to the android API, however, this flag isn't recommended for normal use:

Do not use this flag unless you are implementing your own top-level application launcher. ...

Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.

This information seems as confusing as the rest of the descriptions about intents.

There are other intents flags that I've seen, like Intent.FLAG_ACTIVITY_CLEAR_TASK that achieve a similar result, but they have weird behaviors and/or they use high API

The question

Is it safe to use this flag? Are there any good alternatives to it?

What is the danger of using this flag and what is the meaning of the description on the API of using it?

解决方案

In your first scenario, if your app needs to start an activity of another app, it can just start this activity within the same task as your application. There is no reason to use any special Intent flags for this (you don't need FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_MULTIPLE_TASK). In step 5 of your scenario, the user will return to your application (because BACK just takes him to the previous activity in the current task). This is the standard, default behaviour.

In your second scenario, my first response is "that isn't the standard behaviour". That means that users probably won't expect to be able to go back to the task that they were doing prior to clicking on the notification. However, if you really want to implement this, then I would suggest that you create a special Activity that is launched from the notification and this special Activity should have a different taskAffinity than the rest of the Activities in your application. In this case, when the special Activity is launched from the notification, it will not bring your application's task to the foreground. It will just create a new task containing just the special Activity. When the special Activity is showing, the user can press the BACK key and this will return him to the task that he was working on prior to clicking on your notification.

In general you should NOT use FLAG_ACTIVITY_MULTIPLE_TASK. The main reason is that if you have several tasks containing your application (or parts of it), it is pretty much impossible for the user to return to a specific one. There is no way to provide different launch icons or different application names (for the different tasks)(, so that the user will see multiple tasks in the "recent tasks" list, but will not be able to tell which one is which. You will have a hard time cleaning up what you are doing and you will just make more problems than you can deal with. There are about a million side effects of using this flag and for general applicaitons there is just no need to do it.

这篇关于使用Intent.FLAG_ACTIVITY_MULTIPLE_TASK是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆