kotlin 启动Activity优雅的启动方式

来自于 wanandroid,做个记录

inline fun  Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null) =
    startActivity(getIntent(flags, extra))

inline fun  Fragment.startKtxActivity(flags: Int? = null, extra: Bundle? = null) =
    activity?.let {
        startActivity(activity?.getIntent(flags, extra))
    }

inline fun  Context.startKtxActivity(flags: Int? = null, extra: Bundle? = null) =
    startActivity(getIntent(flags, extra))


inline fun  Activity.startKtxActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair?
): Unit =
    startActivity(getIntent(flags, extra, arrayListOf(value)))

inline fun  Fragment.startKtxActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair?
) =
    activity?.let {
        startActivity(activity?.getIntent(flags, extra, arrayListOf(value)))
    }

inline fun  Context.startKtxActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    value: Pair?
): Unit =
    startActivity(getIntent(flags, extra, arrayListOf(value)))

inline fun  Activity.startKtxActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    values: List?>?
): Unit =
    startActivity(getIntent(flags, extra, values))

inline fun  Fragment.startKtxActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    values: List?>?
) =
    activity?.let {
        startActivity(activity?.getIntent(flags, extra, values))
    }

inline fun  Context.startKtxActivity(
    flags: Int? = null,
    extra: Bundle? = null,
    values: List?>?
) =
    startActivity(getIntent(flags, extra, values))

inline fun  Activity.startKtxActivityForResult(
    requestCode: Int,
    flags: Int? = null,
    extra: Bundle? = null
): Unit =
    startActivityForResult(getIntent(flags, extra), requestCode)

inline fun  Fragment.startKtxActivityForResult(
    requestCode: Int,
    flags: Int? = null,
    extra: Bundle? = null
) =
    activity?.let {
        startActivityForResult(activity?.getIntent(flags, extra), requestCode)
    }

inline fun  Context.getIntent(flags: Int?, extra: Bundle?): Intent =
    Intent(this, T::class.java).apply {
        flags?.let { setFlags(flags) }
        extra?.let { putExtras(extra) }
    }

inline fun  Context.getIntent(
    flags: Int?,
    extra: Bundle?,
    pairs: List?>?
): Intent =
    Intent(this, T::class.java).apply {
        flags?.let { setFlags(flags) }
        extra?.let { putExtras(extra) }
        pairs?.let {
            for (pair in pairs)
                pair?.let {
                    val name = pair.first
                    when (val value = pair.second) {
                        is Int -> putExtra(name, value)
                        is Byte -> putExtra(name, value)
                        is Char -> putExtra(name, value)
                        is Short -> putExtra(name, value)
                        is Boolean -> putExtra(name, value)
                        is Long -> putExtra(name, value)
                        is Float -> putExtra(name, value)
                        is Double -> putExtra(name, value)
                        is String -> putExtra(name, value)
                        is CharSequence -> putExtra(name, value)
                        is Parcelable -> putExtra(name, value)
                        is Array<*> -> putExtra(name, value)
                        is ArrayList<*> -> putExtra(name, value)
                        is Serializable -> putExtra(name, value)
                        is BooleanArray -> putExtra(name, value)
                        is ByteArray -> putExtra(name, value)
                        is ShortArray -> putExtra(name, value)
                        is CharArray -> putExtra(name, value)
                        is IntArray -> putExtra(name, value)
                        is LongArray -> putExtra(name, value)
                        is FloatArray -> putExtra(name, value)
                        is DoubleArray -> putExtra(name, value)
                        is Bundle -> putExtra(name, value)
                        is Intent -> putExtra(name, value)
                        else -> {
                        }
                    }
                }
        }
    }

你可能感兴趣的:(kotlin)