There are times when there’s multiple lines of operations on the same object, and we do it by calling myObject instance everytime we do any operation. This makes our code repetitive and makes it look bad and ugly. For example, when we put different values in the Intent, we do it like this: var intent = Intent() intent.putExtra("myInt", 0) intent.putExtra("myBool", false) intent.putExtra("myString", "hello string") startActivity(intent) And when we receive this Intent in any other Activity, then we get all the values like this:

Continue reading

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. But, with Kotlin DSL and writing a handful of extension methods, this can become a lot more easier and readable than the traditional ways. Let’s look at the typical traditional way of starting another activity var intent = Intent(myActivity, TargetActivity::class) myActivity.

Continue reading

Often times, there are situation where we don’t know much parameters or arguments would be passed in method. This is done in Java using three dots like this: void anySampleMethod(Int... intList) // Note the three dots after the type { // Now you can access these in for loop. for (Int myInt : intList) { // Do anything with myInt here myInt += 10; } } // When calling this method anySampleMethod(1,2,3,4); // Passed 4 arguments anySampleMethod(2); // Passed 1 argument To do this in Kotlin, we will use vararg which means variable arguments.

Continue reading

While looking through answers on StackOverflow or codes through GitHub about Kotlin, you might have noticed keywords like inline, noinline or crossinline in the methods signatures. Today, I learned about what inline is really about. And will explore other types later. What is inline method, then? Say you have a higher order function in Kotlin, fun higherOrderFunction(callback: () -> Unit) { doSomething() callback() doAnotherThing() } Now, when this is converted to Java, this will look something like this:

Continue reading

When it comes to debugging tools and libraries for Android such as Stetho or Chuck or the Jake Wharton’s great Leak Canary, there’re almost separate modules for debug and release builds. For example, when you add Leak Canary in your app, you will be adding dependencies like this: debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' Please note that there are two separate modules or AARs for debug and release. Its a common practice to call release variants for debugging and inspection tools as NO OP or No Operations.

Continue reading

We often use View’s visibility in our apps to show and hide them. We use void setVisibility(int visibility) method for that purpose. But have you ever thought that why this method always takes VISIBLE, INVISIBLE and GONE rather than any int value like 0 or 1 etc.? Although method’s parameter type is int, then why it doesn’t accept the direct numbers or any other int variable except those three. In java, enum is known concept, and in many cases you can use it, but for android, enum is something you should avoid to use as it’s processing performance is not efficient, so in Android performance patterns it’s told to avoid enums and to use annotations like @IntDef and @StringDef.

Continue reading

A collection of concise write-ups on the things I learn day to day In this age of busy life with smartphones and laptops, we have almost started taking things we learn on daily basis for granted. Whether its just a minor semicolon missing error or any run-time crash exception, we learn lots of things in just coding for an hour even. These things and tips are so small that we don’t give much value to these and next day or day after that, we almost search for same thing again.

Continue reading

Author's picture

Wajahat Karim

Android Developer. UI/UX Designer. Blogger. Writer. Wantrepreneur. GitHub Geek. Tea Lover

Senior Android Developer

Karachi, Pakistan