java - 如何在 Kotlin 中组合 Intent 标志

我想像下面在 Android 中那样组合两个 Intent 标志:

Intent intent = new Intent(this, MapsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);

我尝试做这样的事情,但它对我不起作用:

val intent = Intent(context, MapActivity::class.java)
intent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)

最佳答案

说明:

应用于标志的操作是按位或。在 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.

Source

这里是 Int 的所有位运算列表和 Long

  • shl(bits) – 有符号左移(Java 的 <<)
  • shr(bits) – 有符号右移(Java 的 >>)
  • ushr(bits) – 无符号右移(Java 的 >>>)
  • and(bits) – 按位与(Java 的 &)
  • or(bits) – 按位或(Java 的 |)
  • xor(bits) – 按位异或(Java 的 ^)
  • inv() – 按位反转(Java 的 ~)

解决方案:

因此,在您的情况下,您只需调用 or在你的论点之间,就像这样。

intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK

https://stackoverflow.com/questions/45995425/

相关文章:

generics - 如何在 Kotlin 中获取泛型参数类

kotlin - 警告 : API 'variant.getPackageLibrary()' is

kotlin - 使用 Gradle 和 Kotlin 构建自执行 JAR

android - 无法使用 Android Studio 3.0 + DataBinding +

interface - 如何创建接口(interface)的匿名实现?

kotlin - 实现Java接口(interface)时如何克服 "same JVM signat

javascript - Kotlin JavaScript 到 TypeScript 定义文件

arrays - Kotlin中数据类的equals方法

kotlin - 意外覆盖 : The following declarations have th

java - Kotlin:使用 lambda 代替函数式接口(interface)?