Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin with Android: Base classes and Kotlin Android Extensions

So i have used Kotlin Android Extensions and i find it very easy to use and well worth it. No more findViewById or Butterknife.Bind(...). I have found no issue with it all all except for one situation.

In a base class, for example, BaseActivity, there's normally a bunch of views that will be present in all the layouts, for example, a toolbar. And common operations like changeToolbarColor(), or setToolbarTitle().

In this simple cases, i canno't use Kotlin Android Extensions because, as it is a base class, the view itself will be present on multiple layouts and tho can't be property imported. In this cases i just simply use by lazy {find<>(...).

Is there any way this can be accomplished with the build-in android extension plugin?

like image 451
johnny_crq Avatar asked Mar 29 '16 20:03

johnny_crq


People also ask

What is Kotlin Android extensions?

The Kotlin Android Extensions is a compiler plugin that allows you to access views from your XML directly in activities, fragments and views using what they refer to as synthetic properties. To use it, ensure that the plugin is enabled in your module's build.gradle file: apply plugin: 'kotlin-android-extensions'

What are Android extensions?

Extensions provide a means of associating additional attributes with users or public keys and for managing a certification hierarchy. The extension format also allows communities to define private extensions to carry information unique to those communities.

How do I extend my class with Kotlin?

In Kotlin we use a single colon character ( : ) instead of the Java extends keyword to extend a class or implement an interface. We can then create an object of type Programmer and call methods on it—either in its own class or the superclass (base class).


1 Answers

Kotlin Android Extensions generates an extension function for each element on a given layout. Since extension methods exist outside the inheritance model, there is no way to define a common protocol like abstract val toolbar:Toolbar on the parent.

However, under the hood the extension methods only execute findById, if the given ID exists on a layout it will fetch the element. This means that if you maintain the same ids for your common elements inside your layouts (i.e.: all toolbars with @id/toolbar), you can create a dummy layout with your common elements and their respective IDs. This layout will work as a sort of interface, allowing you to do import kotlinx.android.synthetic.main.base_activity_dummy.* and thus generating the extension methods you want.

By doing the above, the this.toolbar on your BaseActivity will fetch the actual item on your concrete activity instead of the element on the dummy layout.

Of course, this technique, while convenient, is error prone and could make your program very confusing for an outsider. But again, it won't be more error prone than calling findById everywhere.

like image 135
Logain Avatar answered Oct 05 '22 18:10

Logain