java 如何在 Kotlin 中组合意图标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45995425/
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
How to combine Intent flags in Kotlin
提问by FaisalAhmed
I want to combine two intent flags as we do bellow in android
我想结合两个意图标志,就像我们在 android 中做的那样
Intent intent = new Intent(this, MapsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
I tried doing something like this but it didn't work for me
我尝试做这样的事情,但它对我不起作用
val intent = Intent(context, MapActivity::class.java)
intent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
回答by Willi Mentzel
Explanation:
解释:
The operation that is applied to the flags is a bitwise or. In Java you have the |
operator for that.
应用于标志的操作是按位或。在 Java 中,您有相应的|
运算符。
As of bitwise operations [in Kotlin], there're no special characters for them, but just named functions that can be called in infix form.
至于按位运算 [在 Kotlin 中],它们没有特殊字符,只有可以以中缀形式调用的命名函数。
Here a list of all bitwise operations for Int
and Long
这里所有的位操作的列表Int
和Long
shl(bits)
– signed shift left (Java's<<
)shr(bits)
– signed shift right (Java's>>
)ushr(bits)
– unsigned shift right (Java's>>>
)and(bits)
– bitwise and (Java's&
)or(bits)
– bitwise or (Java's|
)xor(bits)
– bitwise xor (Java's^
)inv()
– bitwise inversion (Java's~
)
shl(bits)
– 有符号左移(Java 的<<
)shr(bits)
– 带符号的右移(Java 的>>
)ushr(bits)
– 无符号右移(Java 的>>>
)and(bits)
– 按位和(Java 的&
)or(bits)
– 按位或(Java 的|
)xor(bits)
– 按位异或(Java 的^
)inv()
– 按位反转(Java 的~
)
Solution:
解决方案:
So, in your case you only need to call or
in between your arguments like so.
所以,在你的情况下,你只需要or
像这样在你的参数之间调用。
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
回答by AlexTa
Try something like following:
尝试类似以下内容:
val intent = Intent(this, MapsActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
回答by Gibolt
Advanced, Reuseable Kotlin:
先进的、可重用的 Kotlin:
In Kotlin or
is the replacement for the Java bitwise or |
.
在 Kotlin 中or
是 Java 按位或|
.
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
If you plan to use your combination regularly, create an Intent extension function
如果您打算定期使用您的组合,请创建一个 Intent 扩展函数
fun Intent.clearStack() {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
You can then directly call this function before starting the intent
然后你可以在开始意图之前直接调用这个函数
intent.clearStack()
If you need the option to add additional flags in other situations, add an optional param to the extension function.
如果您需要在其他情况下添加额外标志的选项,请向扩展函数添加一个可选参数。
fun Intent.clearStack(additionalFlags: Int = 0) {
flags = additionalFlags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}