Last active
April 3, 2023 08:12
-
-
Save wajahatkarim3/d3a728dbb20002dc54ac44bad40e4077 to your computer and use it in GitHub Desktop.
Kotlin Extensions for simpler, easier and funw way of launching of Activities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Kotlin Extensions for simpler, easier and funw way | |
* of launching of Activities | |
*/ | |
inline fun <reified T : Any> Activity.launchActivity ( | |
requestCode: Int = -1, | |
options: Bundle? = null, | |
noinline init: Intent.() -> Unit = {}) | |
{ | |
val intent = newIntent<T>(this) | |
intent.init() | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) | |
{ | |
startActivityForResult(intent, requestCode, options) | |
} else { | |
startActivityForResult(intent, requestCode) | |
} | |
} | |
inline fun <reified T : Any> Context.launchActivity ( | |
options: Bundle? = null, | |
noinline init: Intent.() -> Unit = {}) | |
{ | |
val intent = newIntent<T>(this) | |
intent.init() | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) | |
{ | |
startActivity(intent, options) | |
} else { | |
startActivity(intent) | |
} | |
} | |
inline fun <reified T : Any> newIntent(context: Context): Intent = | |
Intent(context, T::class.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple Activities | |
launchActivity<UserDetailActivity>() | |
// Add Intent extras | |
launchActivity<UserDetailActivity> { | |
putExtra(INTENT_USER_ID, user.id) | |
} | |
// Add custom flags | |
launchActivity<UserDetailActivity> { | |
putExtra(INTENT_USER_ID, user.id) | |
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
} | |
// Add Shared Transistions | |
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, avatar, "avatar") | |
launchActivity<UserDetailActivity>(options = options) { | |
putExtra(INTENT_USER_ID, user.id) | |
} | |
// Add requestCode for startActivityForResult() call | |
launchActivity<UserDetailActivity>(requestCode = 1234) { | |
putExtra(INTENT_USER_ID, user.id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment