Use delegates for a cleaner code instead of BaseActivity in Kotlin

Praveen VK
3 min readAug 23, 2022

--

Base classes

Base classes are classes that contain the common functionality that might be needed across all its derived classes. A common example of this would be BaseActivity which is generally created in most Android projects. All other activities created in the project would be extending it & hence deriving the common functionality. eg

abstract class BaseActivity: AppCompatActivity() {

private var mActionBar: ActionBar? = null

override fun onCreate(savedInstanceState: Bundle?) {
sendStateEvent("onCreated")
mActionBar = actionBar
initActionBar()
super.onCreate(savedInstanceState)
}

private fun sendStateEvent(state: String) {
Log.d(TAG, "State is : $state")
}

private fun initActionBar() {
mActionBar?.setLogo(R.drawable.ic_launcher_background)
mActionBar?.title = "Title"
}

override fun onResume() {
sendStateEvent("resumed")
super.onResume()
}

override fun onPause() {
sendStateEvent("paused")
super.onPause()
}

}

In the above example, the base activity logs the state of the activity & initializes ActionBar. The activities extending it would get therefore inherit this & can avoid redundant code of logging and initializing ActionBar.

What’s the problem? Why change now?

As the saying goes “If ain’t broke, don’t fix it”. So why should we migrate to delegates instead of BaseActivity? “The only constant in life is change”. Jokes apart, BaseActivity doesn’t tell us what all it contains. Also, there may be a set of Activities that only need some set of functions and others that need another set of functions. When we go with the approach of extending BaseActivity all functions would be made available to all the Activities. That being said also over a long duration of development your BaseAcitivity would become so cluttered that you won’t know what all does it do? Won’t it be better to have a separate class for handling each set of functions? That’s where delegates come up to rescue!

The Delegate Way

True to its name delegates are components that get delegated tasks instead of a single component as in our case BaseActivity which takes up all the tasks.

The BaseActivity we saw earlier can be replaced by using delegates.

  1. Delegate for logging states
interface StateLogger {
fun registerLifecycleOwner(lifecycleOwner: LifecycleOwner)
}
class StateLoggerImpl: StateLogger, LifecycleEventObserver {

override fun registerLifecycleOwner(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.lifecycle.addObserver(this)
}

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
Log.d(TAG, "State is : ${event.name}")
}

fun sendStateEvent(state: String) {
Log.d(TAG, "State is : $state")
}
}

2. Delegate for ActionBar

interface ActionBarHandler {
fun setActionBar(actionBar: ActionBar?)
}
class ActionbarHandlerImpl: ActionBarHandler {

private var mActionbar: ActionBar? = null

override fun setActionBar(actionBar: ActionBar?) {
mActionbar = actionBar
initToolbar()
}

private fun initToolbar() {
mActionbar?.setLogo(R.drawable.ic_launcher_background)
mActionbar?.title = "SampleTitle"
}

fun setTitle(title: String) {
mActionbar?.title = title
}
}

As you can see we have 2 delegates now instead of one BaseActivity. This for one makes the code more readable as we know what is the purpose of each delegate. Also, this helps us implement only the delegates we would need in the derived Activity. eg if we don’t need to log states of an Activity we can avoid implementing StateLogger for it.

Sometimes you want to have the idli with sambar & sometimes with chutney. At times you may want to go crazy if you know what I mean!

That’s a Wrap!

Two better than one. Bite what you like!

If you want to check out the full code with MainActivity you can check the code snippet

You can also read more about Delegates from the official Kotlin Official Doc or watch the video Delegation In Kotlin by Philipp Lackner.

You can also read my previous article on Sealed Classes.

--

--