How to combine Intent flags in Kotlin

后端 未结 3 2042
花落未央
花落未央 2020-12-05 13:02

I want to combine two intent flags as we do bellow in android

Intent intent = new Intent(this, MapsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEA         


        
3条回答
  •  心在旅途
    2020-12-05 13:22

    Advanced, Reuseable Kotlin:

    In Kotlin or is the replacement for the Java bitwise or |.

    intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
    

    If you plan to use your combination regularly, create an Intent extension function

    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
    }
    

提交回复
热议问题