简体   繁体   English

为什么同时使用Intent.FLAG_ACTIVITY_NEW_TASK和Intent.FLAG_ACTIVITY_SINGLE_TOP?

[英]Why use both Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_SINGLE_TOP?

when a push notification is clicked I use something like that to initiate the intent I want: 当点击推送通知时,我使用类似的东西来启动我想要的意图:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
ctx.startActivity(intent);

It's part of code I've taken from a tutorial but I didn't really understand the use of both FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_SINGLE_TOP . 这是我从教程中获取的代码的一部分,但我并不真正理解FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_SINGLE_TOP

From the docs: 来自文档:

FLAG_ACTIVITY_NEW_TASK(Added in API level 1): FLAG_ACTIVITY_NEW_TASK(在API级别1中添加):

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; 使用此标志时,如果任务已在您正在启动的活动上运行,则不会启动新活动; instead, the current task will simply be brought to the front of the screen with the state it was last in. 相反,当前任务将简单地以最后一个状态被带到屏幕的前面。

FLAG_ACTIVITY_SINGLE_TOP(Added in API level 4): FLAG_ACTIVITY_SINGLE_TOP(在API级别4中添加):

If set, the activity will not be launched if it is already running at the top of the history stack. 如果设置,则如果活动已在历史堆栈的顶部运行,则不会启动该活动。

Both sounds alike to me, I mean it sounds like the NEW_TASK makes sure that if your activity already exists within a task, then a new task won't be created but the old task appears(as I understand, can appear with different activity on top), SINGLE_TOP will only launch the activity if it's not at the top of the stack(so is it means that there will be 2 or more instances of the same activity)? 这两个声音对我来说都很相似,我的意思是听起来像NEW_TASK确保如果您的活动已经存在于任务中,那么将不会创建新任务但是旧任务出现(据我所知,可能会出现不同的活动)顶部), SINGLE_TOP只会启动活动,如果它不在堆栈的顶部(那么是否意味着将有2个或更多相同活动的实例)?

My question: Is there a difference between them that require the use of both? 我的问题:他们之间是否存在需要同时使用两者的区别?

AFAIK 据我所知

FLAG_ACTIVITY_SINGLE_TOP doesn't create a new task to launch itself. FLAG_ACTIVITY_SINGLE_TOP不会创建新任务来启动自身。 It always relies on the previous activity's task to launch itself (along with this, it does check if there is an instance already in the stack and show that if available. In either way, it doesn't create a new task) 它始终依赖于先前活动的任务来启动自身(与此一起,它确实检查堆栈中是否已存在实例并显示它是否可用。无论哪种方式,它都不会创建新任务)

where as 在哪里

FLAG_ACTIVITY_NEW_TASK does create a new task unless there is a task with the same activity. 除非存在具有相同活动的任务,否则FLAG_ACTIVITY_NEW_TASK会创建新任务。

I know it kinda feels the same but the key difference here is when an activity is launched with FLAG_ACTIVITY_SINGLE_TOP for the FIRST TIME , it relies on existing task and FLAG_ACTIVITY_NEW_TASK creates a new task 我知道它有点相同但这里的关键区别是当第一次使用FLAG_ACTIVITY_SINGLE_TOP启动活动 ,它依赖于现有任务而FLAG_ACTIVITY_NEW_TASK创建一个新任务

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

相关问题 无法使用Intent。FLAG_ACTIVITY_NEW_TASK - Cannot Use Intent.FLAG_ACTIVITY_NEW_TASK 为什么我不能在PendingIntent中使用Intent.FLAG_ACTIVITY_NEW_TASK - Why can't I use Intent.FLAG_ACTIVITY_NEW_TASK in PendingIntent 使用标志Intent.FLAG_ACTIVITY_NEW_TASK从onPostExecute()方法启动意图不起作用 - Starting intent from onPostExecute() method with a flag Intent.FLAG_ACTIVITY_NEW_TASK is not working 替代Intent.FLAG_ACTIVITY_CLEAR_TASK - Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK Intent.FLAG_ACTIVITY_CLEAR_TOP 不起作用 - Intent.FLAG_ACTIVITY_CLEAR_TOP is not working 使用 Intent.createChooser 并出现错误:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志 - Using Intent.createChooser and getting error: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag 你如何使用Intent.FLAG_ACTIVITY_CLEAR_TOP清除活动堆栈? - How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack? Android Intent.FLAG_ACTIVITY_CLEAR_TOP是否清除SharedPreferences? - Android Intent.FLAG_ACTIVITY_CLEAR_TOP clears SharedPreferences? 按意图发送信息并使用FLAG_ACTIVITY_CLEAR_TOP - Sending information IN Intent and using FLAG_ACTIVITY_CLEAR_TOP SherlockActivity中的Intent.FLAG_ACTIVITY_CLEAR_TOP无法达到目的 - Intent.FLAG_ACTIVITY_CLEAR_TOP in SherlockActivity not serving the purpose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM