Exploring Kotlin initialization with Android custom views
A closer look at the relationship between Kotlin and programmatic/dynamic view inflation
Today, we explore the question “Where does initialization occur around the lifecycle of a view?” This is topic my teammates hotly debated over for months, so I’ve decided to see for myself. This blurb covers two ways of inflating a custom view for comparison: via layout resource and programmatically.
For this exploration, we look at a small compass application. For convenience, you can download the code on Github to follow along/run the app on your own device.
This app uses View Binding and a custom view to spin the needle based on where the user’s device is facing. The two different cases exist on different branches from the main
branch.
When are Kotlin constructors and init blocks called?
Like Java, Kotlin can declare multiple constructors, but makes a differentiation between primary and secondary constructors. Both are denoted with the keyword constructor
.
In most cases, a Kotlin class will only use a primary constructor. If there are no visibility modifiers or annotations, then a class can omit the constructor
keyword. A Kotlin class without a primary constructor exists will generate one that does nothing.
A primary constructor may also include an initializer block denoted with the keyword init
. The initializer block executes the logic as soon as the class is initialized as part of the primary constructor.
Secondary constructors are mostly used for Java interoperability. For the case of CompassView
below, no primary constructor is declared, but multiple secondary constructors are:
These secondary constructors will delegate to the proper constructor in the super class, or find one that does. But in what order, specifically, would a secondary constructor execute in relation to the primary constructor?
From Kotlin official documentation:
Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks and property initializers is executed before the secondary constructor body.
In CompassView
, the initialization block executes before the secondary constructors do. But where does Kotlin class initialization fall in relation to the View lifecycle, exactly? We answer this by examining how a view is inflated.
What happens when a view is created?
The answer depends on how a view is added in a tree. On the screen, all views in Android exists in a single tree. You can add views to that tree two ways:
- Programmatically: adding a View to that tree.
- XML: specifying another tree to via an Android layout file.
In our compass application, we inflate our custom CompassView
on to the MainActivity
as the only custom view component. This article demonstrates the differences between inflating a custom view both via XML (CompassView(context, attrs)
) and code(CompassView(context)
).
Case 1: Custom Kotlin View inflated via XML
When you put a view element into an XML file, Android will use a LayoutInflater
to parse and map the corresponding objects in the XML to the inflated Views. LayoutInflater
retrieves the resource
by opening up ResourceManager and examining all the possible layouts it could match within its current configuration. Android will then parse back the appropriate binary XML resource.
For CompassView(context, attrs)
the second argument will utilize the properties necessary to tell the view hierarchy how to size and place the element in the tree. This post does not focus on these phases (Measure/Layout/Draw) of the View
lifecycle, but there is a totally awesome talk from Drawn out: How Android renders (Google I/O ’18) that dives deep down into the mechanisms for whomever is curious.
This process of inflation is then repeated for its children and its children’s children, recursively until all views have been inflated. When inflation is complete, LayoutInflater
sends a callbackonFinishInflate()
from the children views back up to the root to indicate the view is ready to interact with.
Now that we’ve described the process a bit more, let’s examine the code needed to instantiate our custom CompassView
with XML:
In MainActivity
, the CompassView
via the view binding reference from the XML. When initializing the custom view this way, the default secondary constructor is executed:
D/Compass_View_Kotlin: Kotlin init block called. CompassView(context, attrs) calledD/Compass_View_Kotlin: Inflation started from constructor.D/Compass_View_Kotlin: onFinishInflate() called.D/MainActivity: onStart(): Start compass.
The Kotlin initialization block is called first, then the secondary constructor CompassView(context, attrs)
. Remember, there is no primary constructor for extending a View class (unless you create one yourself), so it makes sense that init
is executed first before the secondary constructor does. Because calling CompassView(context, attrs)
uses LayoutInflater
, the onFinishInflate()
callback is made when all views have finished inflating.
Case 2: Custom Kotlin View inflated programmatically
For the most part, initializing views via XML is the preferred way of creating view elements in Android since it becomes part of the tree view hierarchy. There are advantages to this: using XML is much more friendly for Android memory since it can compress easily, and helps to take off runtime workload a programmatic draw might have to do.
Suppose you have a case where you cannot include the creation of a custom view in the XML, but rather, you must initialize the view only at runtime.
This is what the code might look like:
As you can see, there’s a significant work load setting up the UI programmatically just to make the CompassView
element fit in with ConstraintLayout. This particular case would be incredibly impractical in real life, but there is a special reason we talk about initializing CompassView(this)
. To demonstrate, we’ll run this code:
D/Compass_View_Kotlin: Kotlin init block called.CompassView(context) calledD/Compass_View_Kotlin: Inflation started from constructor.D/MainActivity: onStart(): Start compass.
Like the previous case, the Kotlin initialization block executes first, then the secondary constructor CompassView(context)
. However, you might have noticed that no onFinishInflate()
callback is ever made. This is because CompassView(context)
is not instantiated by XML, meaning that no LayoutInflater
is put into play. There’s no children to wait on for recursive instantiation, and so there’s no onFinishInflate()
for callback.
If you needed to add shared logic for different constructor calls that doesn’t depend on this system call, this might pose a problem for creating more manual view bindings in older code and other UI-related actions.
The Recommendation
You can annotate a custom view with the @JvmOverloads
annotation, which tells the Kotlin compiler to generate overloaded methods. Every overload will substitute with default values for any parameters omitted in the generated methods, which means that the code below is equivalent to the CompassView
class constructors written in the beginning of this article:
Instead of depending on the system to call onFinishInflate
, you can use Kotlin init{ }
for shared logic (even with views dynamically inflated in the code). Running the inflate
method in the initialization block will guarantee inflation occurs no matter how you choose to initialize your custom view.
I hope you enjoyed this blurb: in Android, looking at certain concepts through the lens of Kotlin can shed new perspective to seemingly basic questions. You can find the recommended version of the code on the main
project, and additional resources below.
Additional Resources:
- Droidcon NYC 2016 : How LayoutInflater works
- Layout Inflater: https://developer.android.com/reference/android/view/LayoutInflater
- Kotlin initialization: https://kotlinlang.org/docs/classes.html