如何在 Kotlin 中组合意图标志 [英] How to combine Intent flags in Kotlin

查看:27
本文介绍了如何在 Kotlin 中组合意图标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合两个意图标志,就像我们在 Android 中所做的那样:

I want to combine two intent flags as we do below in 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)

推荐答案

说明:

应用于标志的操作是按位或.在 Java 中,您可以使用 | 运算符.

The operation that is applied to the flags is a bitwise or. In Java you have the | operator for that.

至于 [在 Kotlin 中] 的按位运算,没有特殊字符对它们来说,只是可以以中缀形式调用的命名函数.

As of bitwise operations [in Kotlin], there're no special characters for them, but just named functions that can be called in infix form.

来源

这里列出了 IntLong

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

解决方案:

因此,在您的情况下,您只需要像这样在参数之间调用 .

So, in your case you only need to call or in between your arguments like so.

intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK

这篇关于如何在 Kotlin 中组合意图标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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