All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.catchpig.utils.ext.ActivityExt.kt Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
package com.catchpig.utils.ext

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.activity.result.ActivityResult

inline fun  Context.startKtActivity(
    intent: Intent = Intent()
) {

    intent.setClass(this, T::class.java)
    startActivity(intent)
}

inline fun  Fragment.startKtActivity(
    intent: Intent = Intent()
) {
    activity?.let {
        intent.setClass(it, T::class.java)
        it.startActivity(intent)
    }
}

inline fun  AppCompatActivity.startKtActivity(
    intent: Intent = Intent(),
    crossinline callback: (ActivityResult) -> Unit
) {
    intent.setClass(this, T::class.java)
    registerForActivityResult(StartActivityForResult()) {
        callback(it)
    }.launch(intent)
}

inline fun  Fragment.startKtActivity(
    intent: Intent = Intent(),
    crossinline callback: (ActivityResult) -> Unit
) {
    activity?.let {
        intent.setClass(it, T::class.java)
        registerForActivityResult(StartActivityForResult()) { result ->
            callback(result)
        }.launch(intent)
    }
}

/**
 * startActivityForResult已经被废弃
 * @receiver Activity
 * @param requestCode Int
 * @param intent Intent
 */
@Deprecated(
    "当前方法已废弃,请使用startKtActivity",
    replaceWith = ReplaceWith(
        expression = "startKtActivity(intent){\n}"
    )
)
inline fun  Activity.startKtActivityForResult(
    requestCode: Int,
    intent: Intent = Intent()
) {
    intent.setClass(this, T::class.java)
    startActivityForResult(intent, requestCode)
}

/**
 * startActivityForResult已经被废弃
 * @receiver Fragment
 * @param requestCode Int
 * @param intent Intent
 */
@Deprecated(
    "当前方法已废弃,请使用startKtActivity",
    replaceWith = ReplaceWith(
        expression = "startKtActivity(intent){\n}",
    )
)
inline fun  Fragment.startKtActivityForResult(
    requestCode: Int,
    intent: Intent = Intent()
) {
    activity?.let {
        intent.setClass(it, T::class.java)
        it.startActivityForResult(intent, requestCode)
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy