What should the View layer be responsible for in Android?
Tags: android, softwareengineering
Published: 2022-04-23 17:03:17

---
title: What should the View layer be responsible for in Android?
published: true
date: 2022-04-22 07:01:23 UTC
tags: Android,Softwareengineering
canonical_url: https://www.rockandnull.com/android-view/
---

 ![What should the View layer be responsible for in Android?](https://images.unsplash.com/photo-1605116702570-c7c9c554c8d6?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDJ8fGJvdW5kYXJpZXN8ZW58MHx8fHwxNjUwNjEwNzQx&ixlib=rb-1.2.1&q=80&w=2000)

Recently, the excellent "[Guide to app architecture](https://developer.android.com/jetpack/guide)" was released. This is an awesome resource for anyone entering the Android app development field. It summarises many of the important architectural decisions that need to be taken in mind when creating a new Android app. Whatever your experience level may be, a quick read definitely deserves your time.

Of course, when it comes to software architecture, there's no truly a single universal way of doing things. And the "borders" between layers, no matter how hard you might try to describe them, are subjective.

In the "[Types of logic](https://developer.android.com/jetpack/guide/ui-layer#logic-types)" section, the "UI business logic" is defined as:

> **UI behavior logic** or **UI logic** is _ **how** _ to display state changes on the screen. Examples include obtaining the right text to show on the screen using Android [`Resources`](https://developer.android.com/reference/android/content/res/Resources), navigating to a particular screen when the user clicks a button, or displaying a user message on the screen using a [toast](https://developer.android.com/guide/topics/ui/notifiers/toasts) or a [snackbar](https://developer.android.com/training/snackbar).

And this is where I disagree with the authors.

Traditionally, UI testing is quite hard (and fragile) in the Android world. So we end up having quite a strong coverage for non-UI elements of the app, and a few integration tests to test end-to-end the entire app. Moving as much logic outside of the view should be the aim when building an Android app.

In my opinion, obtaining the right strings to display, or navigating to different screens, should not be the responsibility of the view layer. This should be handled in the business logic layer (i.e. View Model, if following the popular MVVM approach). Putting it in the view layer makes it harder to test and reason about. And these are just too important responsibilities to leave untested (or under-tested).

But you might ask what happens with the concept of isolating dependencies to the Android system away from the ViewModel (or another business logic layer)? The View Model should never know anything about Android, if possible, and this is the right approach. This is where abstraction comes into play. Depending on how strongly you want to follow this isolation guideline, you can abstract away as many parts of the Android system by wrapping them with your own classes that can easily be mocked/replaced in tests.

For instance, when I want to assemble some strings for the view layer to show, I need access to the Android's `Resource` object. I tend to create a `SystemController` that can access system resources and can be easily mocked in Unit tests. So the responsibility of gathering and assembling strings stays in View Model. The View will get exactly what needs to be displayed. No "_how_ to display" logic is stored in the View layer, but rather "_where_ to display" logic.

```
@Singleton
class SystemController @Inject constructor(
    @ApplicationContext private val context: Context
) {

    fun getString(@StringRes stringResId: Int): String = context.getString(stringResId)
}

```

Of course, this has the downside that the View Model knows about resource ids, but I am fine with this. If you insist on complete isolation from the Android system, you can go one step further and abstract these resource ids away (which will be more verbose, but doable).

Hopefully, even if you disagree with this approach, this kind of rant was useful food for thought :)

Happy coding!