前言

对Activity和Fragment之间的跳转封装处理,在Activity/Framgent中用法基本一样。每个函数基本上都对应着一个扩展函数,方便调用。实现方式有很多种,可以自由组合,实际使用时按需求使用。

Intent 扩展函数api列表

  1. //传值解析函数
  2. intentK :intent 获取传值扩展函数
  3. //创建 intent
  4. intentOf :生成Intent并添加参数(可选)
  5. createIntent :生成Intent并添加参数(可选)
  6. createActivityIntent:生成跳转的Intent并添加参数,也可以不添加参数
  7. createBroadcastIntent:生成发送广播 intent,可以添加参数
  8. addPair :Intent 添加参数
  9. startActivityK :跳转
  10. startActivityForResultK :带回调的跳转
  11. browse :打开浏览器
  12. share :生调用分享,只能分享文本
  13. email :发送邮件
  14. makeCallPermission :拨打电话,需要权限
  15. makeCall :拨打电话
  16. sendSMS :发送短信

常规跳转startActivity,没有回调

  1. 不带任何参数

    1. IntentKtx.startActivityK(this,TvActivity::class.java)
    2. IntentKtx.startActivityK<TvActivity>(this)
    3. startActivityK<TvActivity>()//扩展函数

    前两个是封装函数,第3个是扩展函数,可以直接在Activity好Fragment中调用,在其他地方只要有Context也可以调用扩展函数,方式如下:

    1. context.startActivityK<TvActivity>()
  2. 使用Intent传参

    1. val intent = Intent(this,TvActivity::class.java)
    2. intent.putExtra("tv","one")
    3. intent.putExtra("hai","hai---")
    4. IntentKtx.startActivityK(this,intent)

    对应的扩展函数:

    1. val intent = Intent(this,TvActivity::class.java)
    2. intent.putExtra("tv","one")
    3. intent.putExtra("hai","hai---")
    4. // IntentKtx.startActivityK(this,intent)
    5. startActivityK(intent)

    更简便的写法: ``` Intent(this,TvActivity::class.java) .addPair(“tv” to “one”, “hai” to “two”) ?.let { startActivityK(it) }

//或者 createIntent( “tv” to “one”, “hai” to “two” ).let { startActivityK(it) }

  1. 3. 使用Pair传参

//函数 IntentKtx.startActivityK(this,TvActivity::class.java,”tv” to “one”,”hai” to “hello”) //扩展函数 startActivityK(TvActivity::class.java,”tv” to “one”,”hai” to “hello”)

  1. ```
  2. //函数
  3. IntentKtx.startActivityK<TvActivity>(this,"tv" to "one","hai" to "hello")
  4. //扩展函数
  5. startActivityK<TvActivity>("tv" to "one","hai" to "hello")

startActivityForResult跳转

使用Fragment处理onActivityResult,网络上有很多开源库都是使用Fragment处理封装。回调result就是onActivityResult里的resultCode, intent就是回传的数据。

  1. 无参跳转

    1. ActivityHelper.init(this)
    2. ?.startActivityForResult(TvActivity::class.java){reuslt,intent->
    3. Log.e(TAG, "btnMainStartListener: $reuslt ---- ${intent?.getStringExtra("key")}");
    4. }

    对应的扩展函数:

    1. startActivityForResultK(TvActivity::class.java){ reuslt ,intent->
    2. Log.e(TAG, "btnMainStartListener: $reuslt ---- ${intent?.getStringExtra("key")}");
    3. }
  2. 无参跳转,使用内联函数

    1. ActivityHelper.init(this)
    2. ?.startActivityForResult<TvActivity>{reuslt,intent->
    3. Log.e(TAG, "btnMainStartListener: $reuslt ---- ${intent?.getStringExtra("key")}");
    4. }

    对应的扩展函:

    1. startActivityForResultK<TvActivity>{result ,intent->
    2. Log.e(TAG, "btnMainStartListener: $result ---${intent?.getStringExtra("key")}")
    3. }
  3. 使用Intent携带参数跳转

    1. //带参数跳转,数据放在 intent 里面
    2. /* val intent2 = Intent(this,TvActivity::class.java)
    3. intent2.putExtra("tv","key one")
    4. ActivityHelper.init(this)
    5. ?.startActivityForResult(intent2){result ,intent->
    6. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    7. }*/
    8. //扩展函数
    9. /* val intent2 = Intent(this,TvActivity::class.java)
    10. intent2.putExtra("tv","key one")
    11. startActivityForResultK(intent2){result ,intent->
    12. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    13. }*/
  4. 使用Pair带参跳转

    1. //pair 传值
    2. /*ActivityHelper.init(this)
    3. ?.startActivityForResult(TvActivity::class.java,
    4. "tv" to "key one", "hai" to "key two"){result ,intent->
    5. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    6. }*/
    7. //扩展函数
    8. /* startActivityForResultK(TvActivity::class.java,
    9. "key" to "key one", "key2" to "key two"){result ,intent->
    10. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    11. }*/
  5. 使用Pair带参跳转,内联函数

    1. //方法一
    2. ActivityHelper.init(this)
    3. ?.startActivityForResult(TvActivity::class.java,{result ,intent->
    4. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    5. },"key" to "key one", "key2" to "key two")
    6. //方法二
    7. ActivityHelper.init(this)
    8. ?.startActivityForResult<TvActivity>("key" to "key one", "key2" to "key two"){result ,intent->
    9. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    10. }
    11. //扩展函数
    12. startActivityForResultK<TvActivity>("tv" to "key one", "hai" to "key two"){result ,intent->
    13. Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
    14. }

在目标Activity/Fragment使用

在目标Activity/Fragment界面,创建函数实现跳转。这样方便其他人调用跳转,而且不用关心传值的key,也方便以后同意修改。这里面无法直接使用扩展函数,有些方法虽然可以使用,但是并不比扩展函数更方便,这里给出几个简单示例,实现方式有很多种,可以自由组合,实际使用时按需求使用。

  1. companion object{
  2. //这里不能直接使用扩展函数,简单几个示例,不带回调跳转
  3. //不传参数一般也没必要在这里写,直接使用扩展函数更方便
  4. fun starter(context: Context){
  5. IntentKtx.startActivityK(context,TvActivity::class.java)
  6. }
  7. fun starter2(context: Context){
  8. IntentKtx.startActivityK<TvActivity>(context)
  9. }
  10. //一般这种没有必要写在这里,直接用扩展函数跳转就好了
  11. fun starter(context: Context,vararg pair: Pair<String,Any?>){
  12. IntentKtx.startActivityK<TvActivity>(context,*pair)
  13. }
  14. //这里比较实现这种情况,key值定好,调用的地方直接传对应的参数就行了
  15. fun starter(context: Context,tv: String?,hai: String?){
  16. val intent = Intent(context,TvActivity::class.java)
  17. intent.addPair("tv" to tv, "hai" to hai)
  18. IntentKtx.startActivityK(context,intent)
  19. }
  20. //带回调跳转
  21. fun starter(context: Context?,callback: ((Int, Intent?) -> Unit)?){
  22. ActivityHelper.init(context)
  23. ?.startActivityForResult<TvActivity>(callback)
  24. }
  25. //回调也一样,这里比较适合这种情况
  26. fun starter(context: Context?,tv: String?,hai: String?,callback: ((Int, Intent?) -> Unit)?){
  27. val intent = Intent(context,TvActivity::class.java)
  28. intent.addPair("tv" to tv, "hai" to hai)
  29. ActivityHelper.init(context)
  30. ?.startActivityForResult(intent,callback)
  31. }
  32. //写成下面这样更
  33. fun starter(context: Context, tv: String?, hai: String?, callback: ((Int, Intent?) -> Unit)?){
  34. val intent = context.createIntent<TextViewActivity>("tv" to tv, "hai" to hai)
  35. ActivityHelper.init(context)
  36. ?.startActivityForResult(intent,callback)
  37. }
  38. //或者写成这样,实现方式有很多,可以自由组合
  39. fun starter2(context: Context, tv: String?, hai: String?, callback: ((Int, Intent?) -> Unit)?){
  40. val intent = context.createIntent<TextViewActivity>("tv" to tv, "hai" to hai)
  41. context.startActivityForResultK(intent,callback)
  42. }
  43. }

其他界面调用示例:

  1. //调用跳转类的跳转方法
  2. // TvActivity.starter(this)
  3. // TvActivity.starter(this,"one","two")
  4. //回调跳转
  5. // TvActivity.starter(this,"one","two"){result,intent ->
  6. // Log.e(TAG, "btnMainStartListener: $result ---- ${intent?.getStringExtra("key")}")
  7. // }

源码

内容比较简单,只有三个类,约400行代码。复制到代码中即可使用。

  • RouterFragment ```kotlin class RouterFragment : Fragment() {

    private val mCallbacks: SparseArray<((Int, Intent?) -> Unit)?> = SparseArray()

    private val mCodeGenerator: Random = Random()

    companion object {

    1. fun newInstance(): RouterFragment? {
    2. return RouterFragment()
    3. }

    }

    override fun onCreate(savedInstanceState: Bundle?) {

    1. super.onCreate(savedInstanceState)
    2. //防止重复创建
    3. retainInstance = true

    }

    fun startActivityForResult(intent: Intent, callback: ((Int, Intent?) -> Unit)?) {

    1. val requestCode = makeRequestCode()
    2. mCallbacks.put(requestCode, callback)
    3. startActivityForResult(intent, requestCode)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

    1. super.onActivityResult(requestCode, resultCode, data)
    2. //处理返回的结果
    3. val callback = mCallbacks[requestCode]
    4. mCallbacks.remove(requestCode)
    5. callback?.run {
    6. invoke(resultCode, data)
    7. }

    }

    /**

    • 随机生成唯一的requestCode,最多尝试10次
    • @return */ private fun makeRequestCode(): Int { var requestCode: Int var tryCount = 0 do {
      1. requestCode = mCodeGenerator.nextInt(0x0000FFFF)
      2. tryCount++
      } while (mCallbacks.indexOfKey(requestCode) >= 0 && tryCount < 10) return requestCode }

}

  1. - IntentKtx
  2. ```kotlin
  3. object IntentKtx {
  4. fun addPair(intent: Intent, vararg params: Pair<String, Any?>) {
  5. params.forEach {
  6. when (val value = it.second) {
  7. null -> intent.putExtra(it.first, null as Serializable?)
  8. is Int -> intent.putExtra(it.first, value)
  9. is Long -> intent.putExtra(it.first, value)
  10. is CharSequence -> intent.putExtra(it.first, value)
  11. is String -> intent.putExtra(it.first, value)
  12. is Float -> intent.putExtra(it.first, value)
  13. is Double -> intent.putExtra(it.first, value)
  14. is Char -> intent.putExtra(it.first, value)
  15. is Short -> intent.putExtra(it.first, value)
  16. is Boolean -> intent.putExtra(it.first, value)
  17. is Serializable -> intent.putExtra(it.first, value)
  18. is Bundle -> intent.putExtra(it.first, value)
  19. is Parcelable -> intent.putExtra(it.first, value)
  20. is Array<*> -> when {
  21. value.isArrayOf<CharSequence>() -> intent.putExtra(it.first, value)
  22. value.isArrayOf<String>() -> intent.putExtra(it.first, value)
  23. value.isArrayOf<Parcelable>() -> intent.putExtra(it.first, value)
  24. else -> throw Exception("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
  25. }
  26. is IntArray -> intent.putExtra(it.first, value)
  27. is LongArray -> intent.putExtra(it.first, value)
  28. is FloatArray -> intent.putExtra(it.first, value)
  29. is DoubleArray -> intent.putExtra(it.first, value)
  30. is CharArray -> intent.putExtra(it.first, value)
  31. is ShortArray -> intent.putExtra(it.first, value)
  32. is BooleanArray -> intent.putExtra(it.first, value)
  33. else -> throw Exception("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
  34. }
  35. }
  36. }
  37. /**
  38. * 不带回调跳转相关
  39. */
  40. fun startActivityK(context: Context?, clazz: Class<*>) {
  41. context?.startActivity(Intent(context, clazz))
  42. }
  43. inline fun <reified T> startActivityK(context: Context?) {
  44. context?.startActivity(Intent(context, T::class.java))
  45. }
  46. fun startActivityK(context: Context?, intent: Intent) {
  47. context?.startActivity(intent)
  48. }
  49. fun startActivityK(context: Context?, clazz: Class<*>, vararg params: Pair<String, Any?>) {
  50. context?.run {
  51. val intent = Intent(this, clazz)
  52. intent.addPair(*params)
  53. startActivity(intent)
  54. }
  55. }
  56. inline fun <reified T> startActivityK(context: Context?, vararg params: Pair<String, Any?>) {
  57. context?.run {
  58. val intent = Intent(this, T::class.java)
  59. intent.addPair(*params)
  60. startActivity(intent)
  61. }
  62. }
  63. }
  64. /**
  65. *生成跳转的Intent并添加参数
  66. * @param T
  67. * @param pair
  68. */
  69. inline fun <reified T> Context.createIntentStart(vararg pair: Pair<String, Any?>) =
  70. Intent(this, T::class.java).apply {
  71. addPair(*pair)
  72. }
  73. /**
  74. *生成Intent添加参数,也可以不添加
  75. */
  76. fun Context.createIntent(vararg pair: Pair<String, Any?>) = Intent().addPair(*pair)
  77. /**
  78. *给Intent添加参数
  79. * @param params
  80. * @return
  81. */
  82. fun Intent?.addPair(vararg params: Pair<String, Any?>): Intent? {
  83. return this?.also { IntentKtx.addPair(it, *params) }
  84. }
  85. /**
  86. * 不带回调,无参跳转
  87. */
  88. fun Context?.startActivityK(clazz: Class<*>) {
  89. this?.startActivity(Intent(this, clazz))
  90. }
  91. inline fun <reified T> Context?.startActivityK() {
  92. this?.startActivity(Intent(this, T::class.java))
  93. }
  94. /**
  95. * 不带回调,带参跳转
  96. */
  97. fun Context?.startActivityK(intent: Intent) {
  98. this?.startActivity(intent)
  99. }
  100. fun Context?.startActivityK(clazz: Class<*>, vararg pair: Pair<String, Any?>) {
  101. this?.run {
  102. val intent = Intent(this, clazz)
  103. startActivity(intent.addPair(*pair))
  104. }
  105. }
  106. inline fun <reified T> Context?.startActivityK(vararg pair: Pair<String, Any?>) {
  107. this?.run {
  108. val intent = Intent(this, T::class.java)
  109. startActivity(intent.addPair(*pair))
  110. }
  111. }
  112. /**
  113. * 不带回调,无参跳转
  114. */
  115. fun Fragment?.startActivityK(clazz: Class<*>) {
  116. this?.startActivity(Intent(activity, clazz))
  117. }
  118. inline fun <reified T> Fragment?.startActivityK() {
  119. this?.startActivity(Intent(activity, T::class.java))
  120. }
  121. /**
  122. * 不带回调,带参跳转
  123. */
  124. fun Fragment?.startActivityK(intent: Intent) {
  125. this?.startActivity(intent)
  126. }
  127. fun Fragment?.startActivityK(clazz: Class<*>, vararg pair: Pair<String, Any?>) {
  128. this?.run {
  129. val intent = Intent(activity, clazz)
  130. startActivity(intent.addPair(*pair))
  131. }
  132. }
  133. inline fun <reified T> Fragment?.startActivityK(vararg pair: Pair<String, Any?>) {
  134. this?.run {
  135. val intent = Intent(activity, T::class.java)
  136. startActivity(intent.addPair(*pair))
  137. }
  138. }
  139. /**
  140. * Context扩展函数跳转
  141. */
  142. fun Context?.startActivityForResultK(clazz: Class<*>, callback: ((Int, Intent?) -> Unit)?) {
  143. ActivityHelper.init(this)?.startActivityForResult(clazz, callback)
  144. }
  145. fun Context?.startActivityForResultK(intent: Intent, callback: ((Int, Intent?) -> Unit)?) {
  146. ActivityHelper.init(this)?.startActivityForResult(intent, callback)
  147. }
  148. inline fun <reified T> Context?.startActivityForResultK(noinline callback: ((Int, Intent?) -> Unit)?) {
  149. ActivityHelper.init(this)?.startActivityForResult<T>(callback)
  150. }
  151. fun Context?.startActivityForResultK(
  152. clazz: Class<*>,
  153. vararg pair: Pair<String, Any?>,
  154. callback: ((Int, Intent?) -> Unit)?
  155. ) {
  156. ActivityHelper.init(this)
  157. ?.startActivityForResult(clazz, callback, *pair)
  158. }
  159. inline fun <reified T> Context?.startActivityForResultK(
  160. vararg pair: Pair<String, Any?>,
  161. noinline callback: ((Int, Intent?) -> Unit)?
  162. ) {
  163. ActivityHelper.init(this)
  164. ?.startActivityForResult<T>(callback, *pair)
  165. }
  166. /**
  167. * 上述方法在 fragment 中扩展
  168. */
  169. fun Fragment?.startActivityForResultK(clazz: Class<*>, callback: ((Int, Intent?) -> Unit)?) {
  170. ActivityHelper.init(this?.context)?.startActivityForResult(clazz, callback)
  171. }
  172. fun Fragment?.startActivityForResultK(intent: Intent, callback: ((Int, Intent?) -> Unit)?) {
  173. ActivityHelper.init(this?.context)?.startActivityForResult(intent, callback)
  174. }
  175. inline fun <reified T> Fragment?.startActivityForResultK(noinline callback: ((Int, Intent?) -> Unit)?) {
  176. ActivityHelper.init(this?.context)?.startActivityForResult<T>(callback)
  177. }
  178. fun Fragment?.startActivityForResultK(
  179. clazz: Class<*>,
  180. vararg pair: Pair<String, Any?>,
  181. callback: ((Int, Intent?) -> Unit)?
  182. ) {
  183. ActivityHelper.init(this?.context)
  184. ?.startActivityForResult(clazz, callback, *pair)
  185. }
  186. inline fun <reified T> Fragment?.startActivityForResultK(
  187. vararg pair: Pair<String, Any?>,
  188. noinline callback: ((Int, Intent?) -> Unit)?
  189. ) {
  190. ActivityHelper.init(this?.context)
  191. ?.startActivityForResult<T>(callback, *pair)
  192. }
  193. /**
  194. *跳转到浏览器
  195. * @param url
  196. * @param newTask
  197. * @return
  198. */
  199. fun Context.browse(url: String, newTask: Boolean = false): Boolean {
  200. return try {
  201. val intent = Intent(Intent.ACTION_VIEW)
  202. intent.data = Uri.parse(url)
  203. if (newTask) {
  204. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  205. }
  206. startActivity(intent)
  207. true
  208. } catch (e: ActivityNotFoundException) {
  209. e.printStackTrace()
  210. false
  211. }
  212. }
  213. /**
  214. *跳转到浏览器
  215. * @param url
  216. * @param newTask
  217. * @return
  218. */
  219. fun Fragment.browse(url: String, newTask: Boolean = false) = activity?.browse(url, newTask)
  220. /**
  221. *原生调用分享,只能分享文本
  222. * @param text
  223. * @param subject
  224. * @param title
  225. * @return
  226. */
  227. fun Context.share(text: String, subject: String = "", title: String? = null): Boolean {
  228. return try {
  229. val intent = Intent(android.content.Intent.ACTION_SEND)
  230. intent.type = "text/plain"
  231. intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject)
  232. intent.putExtra(android.content.Intent.EXTRA_TEXT, text)
  233. startActivity(Intent.createChooser(intent, title))
  234. true
  235. } catch (e: ActivityNotFoundException) {
  236. e.printStackTrace()
  237. false
  238. }
  239. }
  240. /**
  241. *发送邮件
  242. * @param email
  243. * @param subject
  244. * @param text
  245. * @return
  246. */
  247. @SuppressLint("QueryPermissionsNeeded")
  248. fun Context.email(email: String, subject: String = "", text: String = ""): Boolean {
  249. val intent = Intent(Intent.ACTION_SENDTO)
  250. intent.data = Uri.parse("mailto:")
  251. intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
  252. if (subject.isNotEmpty())
  253. intent.putExtra(Intent.EXTRA_SUBJECT, subject)
  254. if (text.isNotEmpty())
  255. intent.putExtra(Intent.EXTRA_TEXT, text)
  256. if (intent.resolveActivity(packageManager) != null) {
  257. startActivity(intent)
  258. return true
  259. }
  260. return false
  261. }
  262. /**
  263. *拨打电话,需要权限permission.CALL_PHONE才能拨打
  264. * @param number
  265. * @return
  266. */
  267. @SuppressLint("MissingPermission")
  268. fun Context.makeCallPermission(number: String): Boolean {
  269. return try {
  270. val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:$number"))
  271. startActivity(intent)
  272. true
  273. } catch (e: Exception) {
  274. e.printStackTrace()
  275. false
  276. }
  277. }
  278. fun Fragment.markCallPermission(number: String) = context?.makeCallPermission(number)
  279. /**
  280. *拨打电话,不需要权限,跳转到拨打电话界面
  281. * @param number
  282. * @return
  283. */
  284. fun Context.makeCall(number: String) {
  285. val uri = Uri.parse("tel:$number")
  286. val it = Intent(Intent.ACTION_DIAL, uri)
  287. startActivity(it)
  288. }
  289. fun Fragment.markCall(number: String) = context?.makeCall(number)
  290. /**
  291. *发送短信
  292. * @param number
  293. * @param text
  294. * @return
  295. */
  296. fun Context.sendSMS(number: String, text: String = ""): Boolean {
  297. return try {
  298. val intent = Intent(Intent.ACTION_VIEW, Uri.parse("sms:$number"))
  299. intent.putExtra("sms_body", text)
  300. startActivity(intent)
  301. true
  302. } catch (e: Exception) {
  303. e.printStackTrace()
  304. false
  305. }
  306. }
  307. fun Fragment.sendSMS(number: String, text: String = "") = context
  • ActivityHelper ```kotlin class ActivityHelper private constructor(activity: FragmentActivity) { private val TAG = “MMMK” var mContext: Activity? = null private var mRouterFragment: RouterFragment? = null

    init {

    1. mContext = activity
    2. mRouterFragment = getRouterFragment(activity)

    }

    companion object { // fun init(activity: FragmentActivity?): ActivityHelper? { // if (activity != null) return ActivityHelper(activity) // return null // }

    1. fun init(context: Context?): ActivityHelper? {
    2. if (context is FragmentActivity) return ActivityHelper(context)
    3. return null
    4. }

    }

  1. private fun getRouterFragment(activity: FragmentActivity): RouterFragment? {
  2. var routerFragment: RouterFragment? = findRouterFragment(activity)
  3. if (routerFragment == null) {
  4. //创建 fragment,加入当前 activity
  5. routerFragment = RouterFragment.newInstance()
  6. val sfm = activity.supportFragmentManager
  7. sfm.beginTransaction().add(routerFragment!!, TAG).commitAllowingStateLoss()
  8. sfm.executePendingTransactions()
  9. }
  10. return routerFragment
  11. }
  12. private fun findRouterFragment(activity: FragmentActivity): RouterFragment? {
  13. //通过 tag 获取 fragment
  14. return activity.supportFragmentManager.findFragmentByTag(TAG) as RouterFragment?
  15. }
  16. /**
  17. * 对Intent跳转,不带参数
  18. */
  19. fun startActivityForResult(
  20. clazz: Class<*>,
  21. callback: ((Int, Intent?) -> Unit)?
  22. ) {
  23. mContext?.run {
  24. val intent = Intent(this, clazz)
  25. startActivityForResult(intent, callback)
  26. }
  27. }
  28. inline fun <reified T> startActivityForResult(noinline callback: ((Int, Intent?) -> Unit)?) {
  29. mContext?.run {
  30. val intent = Intent(mContext, T::class.java)
  31. startActivityForResult(intent, callback)
  32. }
  33. }
  34. /**
  35. *带参数跳转
  36. * @param T
  37. * @param pair
  38. * @param callback
  39. */
  40. inline fun <reified T> startActivityForResult(
  41. vararg pair: Pair<String, Any?>,
  42. noinline callback: ((Int, Intent?) -> Unit)?
  43. ) {
  44. if (mContext == null) return
  45. val intent = Intent(mContext, T::class.java)
  46. IntentKtx.addPair(intent, *pair)
  47. startActivityForResult(intent, callback)
  48. }
  49. inline fun <reified T> startActivityForResult(
  50. noinline callback: ((Int, Intent?) -> Unit)?,
  51. vararg pair: Pair<String, Any?>
  52. ) {
  53. if (mContext == null) return
  54. val intent = Intent(mContext, T::class.java)
  55. IntentKtx.addPair(intent, *pair)
  56. startActivityForResult(intent, callback)
  57. }
  58. fun startActivityForResult(
  59. clazz: Class<*>,
  60. vararg pair: Pair<String, Any?>,
  61. callback: ((Int, Intent?) -> Unit)?
  62. ) {
  63. mContext?.run {
  64. val intent = Intent(this, clazz)
  65. IntentKtx.addPair(intent, *pair)
  66. startActivityForResult(intent, callback)
  67. }
  68. }
  69. fun startActivityForResult(
  70. clazz: Class<*>,
  71. callback: ((Int, Intent?) -> Unit)?,
  72. vararg pair: Pair<String, Any?>
  73. ) {
  74. mContext?.run {
  75. val intent = Intent(this, clazz)
  76. IntentKtx.addPair(intent, *pair)
  77. startActivityForResult(intent, callback)
  78. }
  79. }
  80. /**
  81. * 对Intent跳转,带参数
  82. * @param intent 参数提取放入intent中
  83. * @param callback
  84. */
  85. fun startActivityForResult(intent: Intent, callback: ((Int, Intent?) -> Unit)?) {
  86. mRouterFragment?.run {
  87. startActivityForResult(intent, callback)
  88. }
  89. }

}

  1. <a name="Sr423"></a>
  2. ## 导入依赖
  3. 不想复制更省事的方法,直接依赖即可。依赖如下:<br />[Github](https://github.com/KiWiLss/LUtils)上面有详细的文档。<br />[最新版](https://www.jitpack.io/v/KiWiLss/LUtils.svg)
  4. ```kotlin
  5. allprojects {
  6. repositories {
  7. ...
  8. maven { url 'https://www.jitpack.io' }
  9. }
  10. }
  11. dependencies {
  12. implementation 'com.github.KiWiLss:LUtils:1.1.0'
  13. }

使用前要调用,获取到 Context:

  1. object LUtilsConfig{
  2. lateinit var mContext: Context
  3. fun init(context: Context) {
  4. mContext = context
  5. }