Kotlin 一行代码启动Activity

直接上代码吧,只是自己做记录方便以后查阅

/**启动Activity*/
inline fun  Context.onStart() {
  startActivity(Intent(this, T::class.java))
}

/**启动Activity*/
inline fun  Context.onStart(mKey: String, mBundle: Bundle?) {
  val mIntent = Intent(this, T::class.java)
  mIntent.putExtra(mKey, mBundle)
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Context.onStart(vararg pair: Pair?) {
  val mIntent = Intent(this, T::class.java)
  pair?.let {
    pair.forEach {
      mIntent.putExtra(it!!.first, it!!.second)
    }
  }
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Activity.onStart() {
  startActivity(Intent(this, T::class.java))
}

/**启动Activity*/
inline fun  Activity.onStart(mKey: String, mBundle: Bundle?) {
  val mIntent = Intent(this, T::class.java)
  mIntent.putExtra(mKey, mBundle)
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Activity.onStart(vararg pair: Pair?) {
  val mIntent = Intent(this, T::class.java)
  pair?.let {
    pair.forEach {
      mIntent.putExtra(it!!.first, it!!.second)
    }
  }
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Activity.onStartResult(requestCode: Int) {
  startActivityForResult(Intent(this, T::class.java), requestCode, null)
}

/**启动Activity*/
inline fun  Activity.onStartResult(requestCode: Int, mBundle: Bundle?) {
  startActivityForResult(Intent(this, T::class.java), requestCode, mBundle)
}

/**启动Activity*/
inline fun  Activity.onStartResult(requestCode: Int, vararg pair: Pair) {
  val mIntent = Intent(this, T::class.java)
  val mBundle = Bundle()
  pair?.let {
    pair.forEach {
      mBundle.putString(it!!.first, it!!.second)
    }
  }
  startActivityForResult(mIntent, requestCode, mBundle)
}

你可能感兴趣的:(Kotlin 一行代码启动Activity)