AndroidPub
Published in

AndroidPub

🚀 Launching Activities in Easier Way Using Kotlin Extensions 💻

Kotlin Extensions for simpler, easier and fun way of launching Activities in Android

Originally published on https://wajahatkarim.com

Few days ago, I was reading this awesome article by Elye about a Kotlin keyword reified . Soon after reading the article, I started exploring it and I realized that we can simply lots of reusable tasks of Android development using reified and Kotlin extension methods. For example, launching Activities.

🔲 Launching Activities — Traditional Way

Launching activities in android apps is a common task and different developers use different approaches. Some use the traditional ways of creating Intent bundles and passing them in startActivity() methods along side the Intents.

Let’s look at the typical traditional way of starting another activity

Now, if we pass some arguments in it, this becomes more complex.

💥 Launching Activities —The Simpler, Easier, and Fun Way

But, creating some Kotlin extension methods with using reifiedkeyword, we can make these a lot simpler and enjoyable.

GitHub Gist: https://gist.github.com/wajahatkarim3/d3a728dbb20002dc54ac44bad40e4077

Now, after this, we can do same launching activity stuff (of previous examples) like this:

Or we can pass arguments like this:

Here are all the cases which you can do with these extensions:

Github Gist: https://gist.github.com/wajahatkarim3/d3a728dbb20002dc54ac44bad40e4077

Wajahat Karim is a graduate from NUST, Islamabad, an experienced mobile developer, an active open source contributor, and co-author of two books Learning Android Intents and Mastering Android Game Development with Unity. In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on his blog and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on Github Trending. His libraries have about 3000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and Medium to get more updates about his work in Writing, Android and Open Source.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store