XContext.kt 615 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package com.yidianling.common.tools

import android.app.Activity
import android.content.Context
import android.view.ContextThemeWrapper
import androidx.fragment.app.FragmentActivity

fun Context.asActivity(): Activity? {
    if (this is Activity) {
        return this
    }
    if (this is ContextThemeWrapper) {
        return this.baseContext.asActivity()
    }
    return null
}

fun Context.asFragmentActivity(): FragmentActivity? {
    if (this is FragmentActivity) {
        return this
    }
    if (this is ContextThemeWrapper) {
        return this.baseContext.asFragmentActivity()
    }
    return null
}