Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin extension function start activity with Intent extras

I am trying to write a kotlin extension function for Context which will start a new activity in android with given class name and list of intent extras. I am able to successfully start activity without any extras but I am facing problem with them.

fun <T> Context.openActivity(it: Class<T>, pairs: List<Pair<String, Any>>) {
  var intent = Intent()
  pairs.forEach {
     intent.putExtra(it.first, it.second)
  }
  startActivity(intent)
}

Main issue here is -> intent.putExtra() doesn't except second param as Any

like image 752
Saksham Khurana Avatar asked Dec 16 '18 15:12

Saksham Khurana


3 Answers

Instead of using a list of pairs, consider using a Bundle. Then you can add it with putExtras(Bundle).

If you want to go one step ahead, you could add a lambda extension to configure the extras:

fun <T> Context.openActivity(it: Class<T>, extras: Bundle.() -> Unit = {}) {
  val intent = Intent(this, it)
  intent.putExtras(Bundle().apply(extras))
  startActivity(intent)
}

Then you can call it as:

openActivity(MyActivity::class.java) {
  putString("string.key", "string.value")
  putInt("string.key", 43)
  ...
}
like image 110
tynn Avatar answered Oct 12 '22 09:10

tynn


Declare:

inline fun <reified T : Activity> Context.startActivity(block: Intent.() -> Unit = {}) {
    startActivity(Intent(this, T::class.java).apply(block))
}

Simple using:

startActivity<MainActivity>()

With extra

startActivity<MainActivity>{
   putExtra("param 1", "Simple")
}
like image 23
Wilson Tran Avatar answered Oct 12 '22 09:10

Wilson Tran


Here is the extension function for start activity:

inline fun <reified T : Activity> Context.openActivity(vararg params: Pair<String, Any>) {
    val intent = Intent(this, T::class.java)
    intent.putExtras(*params)
    this.startActivity(intent)
}

fun Intent.putExtras(vararg params: Pair<String, Any>): Intent {
    if (params.isEmpty()) return this
    params.forEach { (key, value) ->
        when (value) {
            is Int -> putExtra(key, value)
            is Byte -> putExtra(key, value)
            is Char -> putExtra(key, value)
            is Long -> putExtra(key, value)
            is Float -> putExtra(key, value)
            is Short -> putExtra(key, value)
            is Double -> putExtra(key, value)
            is Boolean -> putExtra(key, value)
            is Bundle -> putExtra(key, value)
            is String -> putExtra(key, value)
            is IntArray -> putExtra(key, value)
            is ByteArray -> putExtra(key, value)
            is CharArray -> putExtra(key, value)
            is LongArray -> putExtra(key, value)
            is FloatArray -> putExtra(key, value)
            is Parcelable -> putExtra(key, value)
            is ShortArray -> putExtra(key, value)
            is DoubleArray -> putExtra(key, value)
            is BooleanArray -> putExtra(key, value)
            is CharSequence -> putExtra(key, value)
            is Array<*> -> {
                when {
                    value.isArrayOf<String>() ->
                        putExtra(key, value as Array<String?>)
                    value.isArrayOf<Parcelable>() ->
                        putExtra(key, value as Array<Parcelable?>)
                    value.isArrayOf<CharSequence>() ->
                        putExtra(key, value as Array<CharSequence?>)
                    else -> putExtra(key, value)
                }
            }
            is Serializable -> putExtra(key, value)
        }
    }
    return this
}

Simple using:

openActivity<TestActivity>("key0" to "value0", "key1" to "value1")

A simpler approach is provided in Kotlin

Use the bundleOf()

inline fun <reified T : Activity> Context.openActivity1(vararg params: Pair<String, Any?>) {
    val intent = Intent(this, T::class.java)
    intent.putExtras(bundleOf(*params))
    this.startActivity(intent)
}

like image 37
midFang Avatar answered Oct 12 '22 10:10

midFang