Kotlin has a handful of fundamental differences from Java, not the least of which being the omission of static members and methods. This can cause a bit of a headache when trying to duplicate typical Android patterns using Android’s newest official language.
Skip the prose and get to the code via this Github gist.
In Java Android development it is common to add a static method called newInstance() to a fragment class like this:
The API documentation recommends following this pattern because it allows you to vertically separate the fragment from it’s hosting activity. It keeps your application components modular.
Since the fragment code knows nothing of its hosting activity, you won’t have to deal with refactoring if you decide to host this same fragment elsewhere in the future. All of the information required to create your fragment is self-contained within the fragment code.
Kotlin does not have static members and methods, so duplicating this principle requires some creativity. Here is my method for writing idiomatic Kotlin while maintaining these object-oriented principles.
Kotlin Companion Object
Kotlin introduces the companion object to your Android code, which may sound familiar if you know Scala. When a variable (or function) is declared as part of the companion object you can use it just like you would a static member in Java. The difference here is that companion objects in Kotlin are more versatile. Items declared on the companion object are instances of real objects, which means they can implement interfaces. But they act essentially identical to the Java static objects you know and love.
Here is how you might write fragment code in Kotlin:
Voila! We’ve successfully accomplished the same purpose as our Java code above. The hosting activity can still use
MyFragment.newInstance(), maintaining modularity and reusability. The fragment doesn’t need to know about it’s calling activity and all information about creating the fragment is contained within its own code.
This pattern can also be used for other commonly static data in Android development like
getIntent() or class identifiers.
Object-oriented principles drive quality Android development. Kotlin takes those principles and builds upon them, adding features like null safety, a collection of language tools for using the functional programming paradigm, and interoperability with existing Java code. That all goes without saying that Kotlin is now officially supported by the Android team at Google. Learn more about using Kotlin with Android here.
As is the case with most Android principles, converting your fragments to Kotlin requires a slight learning curve and some experimentation. Please reach out to me with any improvements on my design.