Smart Kotlin function for launching Activities

Malwinder Singh
3 min readJan 18, 2020

--

Small, sweet, Android.

Hello friends ☺️☺️

A very Happy New Year to you..!!

In this post, I shall tell you about a smart Kotlin function to launch Activities.

This function will help you to stop creating an Intent the object again and again but instead do all of your operations using a pre-defined Intent.

And also it will make you look you are a cool and smart coder 😎

And the function is:

/**
* Extension for smarter launching of Activities
*/
inline fun <reified T : Any> Context.launchActivity(
noinline modify: Intent.() -> Unit = {}
) {
val intent = Intent(this, T::class.java)
intent.modify()
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}

Now, let me explain to you how it works.

  1. First, the first:
inline fun <reified T : Any> Context.launchActivity(
noinline modify: Intent.() -> Unit = {}
) {

To make use of reified keyword, we have to make a function inline.

Also, if we want to use modify function (It works similar to apply, a scope function of Kotlin), we have to make modify function as noinline.

inline means that when this file is compiled to Java code (Please note that all of the Kotlin files are converted to Java files which are further compiled down to Java byte code), it does not create a separate function for it.

But, instead, all of the code in that function moves to the place where it is being called. Similarly, noinline is opposite of inline.

It does not allow code to move to the caller function(s).

2. Now, the second:

intent.addFlags(FLAG_ACTIVITY_NEW_TASK)

Why on this earth do we need to do this? Simply because, if you call this function outside of, it will show you the following Runtime exception:

Calling startActivity() from outside of an Activity  context requires the  
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Hence, we need to add this.

Expert Suggestion: You should create a BaseActivity and let all of your Activities in your project to inherit it (You know what it means.. 🙂 ).

And, also modify your function to:

/**
* Extension for smarter launching of Activities
*/
inline fun <reified T : BaseActivity> Context.launchActivity(
noinline modify: Intent.() -> Unit = {}
) {
val intent = Intent(this, T::class.java)
intent.modify()
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}

Now, how to use it?

If you want to launch an Activity simply, then use:

launchActivity<HomeActivity>()

Now comes a little complex part (No, don’t get scared.. 🙂 )

If you want to put some Extras then use this following:

launchActivity<ProfileActivity> { 
putExtra(EXTRA_USER_NAME, "Alan Walker")
putExtra(EXTRA_USER_AGE, 22)
//So on..
}

Hope you liked this article!

Medium does not allow Indian writers to monetize currently 😭. Please support this blog by contributing here: https://www.patreon.com/malwinder 🥰

Buy me a coffee 🥰

Even a small contribution would help 😍

If you have any questions, then please write down a response in the response section.
If you liked this article, then please press the 👏 icon.
Thank you!
Peace! 🙂

--

--

Malwinder Singh

Flutter, Kotlin & Android. Worked on projects of Emerson and Omron, 50k+ views on this blog, 6k+ repo on StackOverflow, Drone startup linkedin.com/in/malwinder