Modernising A Legacy Android App Architecture, Part One: The Single Activity

Background Hello. I’m Rob. I work on the Android version of BBC Sport. Our codebase started its life in 2013 and it’s fair to say we have amassed some tech debt over that time. This is the story of how we modernised a core part of its architecture. It’s aimed at a technical audience who […]


Hello. I’m Rob. I work on the Android version of BBC Sport. Our codebase started its life in 2013 and it’s fair to say we have amassed some tech debt over that time. This is the story of how we modernised a core part of its architecture. It’s aimed at a technical audience who have at least some familiarity with Android.

Obligatory disclaimer: views expressed are my own, not the BBC’s or the team’s.

BBC Sport on Android in December 2020

The fundamentals of the app were not unusual for that era of Android development:

  • multiple activities
  • some use of fragments
  • some use of viewmodels
  • various UI patterns over time: MVP, MVVM, MVI

What we wanted to achieve was:

  • consistency with modern Android development practices
  • consistency with other modern BBC app architectures
  • clarity for developers — being able to look at the codebase and see quickly that this is the pattern for how we build features
  • usual product qualities: maintainability, reliability, extensibility

The absence of some of these characteristics — not all of them, and not totally — was a problem, and for various reasons, this slowly escalated from typical developer gripes to a more pressing business need. The organisation recognised the need to take action, and therefore we as a team were granted some time to address our architectural issues.

Rewrite Or Refactor?

This sometimes got referred to within the business as a ‘rewrite’, and it was of sorts, but it was one within strict parameters: not a start-again-from-scratch development, and an activity with a particular timeframe in mind, at least for this focused phase. Really this meant ‘refactor’ rather than ‘rewrite’.

We have built apps from scratch before in my time at the BBC. Sounds is a good example. Rather than overhauling its predecessor, iPlayer Radio, we started a ‘greenfield’ development and took our time to do it carefully. Though I say so myself, we did that job extremely well and it’s the best engineered software product I’ve worked on, largely the culmination of a lot of other people’s prior experience building BBC apps.

BBC Sounds, written from scratch starting in 2017

For Sounds, a fresh start was the right call, but it was also one driven by lots of factors beyond engineering concerns. It was a reinvention of the product in its entirety — the look and feel, the marketing, the backend and other components far beyond the app.

In our case we’re solving an engineering problem whilst trying to maintain product consistency, for now, and although a greenfield opportunity would be wonderful, you can understand it’s a luxury we can’t always afford. What we were able to do is take experiences like the Sounds development as an ideal model and re-apply many of those core techniques and architectural principles in a compressed and accelerated way.

This overall story is about how we put this into action, and solved at least some of our problems.

This is the first in a short sequence of articles on the subject, and over the course of these, we will run through a few different things in turn:

  • this article: the problems of multiple activities, and getting away from this
  • next article: what we think a good UI design pattern looks like
  • third article: the overall phase of work and some specific experiences we had with doing this

It’s about 10,000 words in total, so quite a lot of detail especially in the last one, but let’s start off simple.

Navigation Soup

When you have lots of Activities, two things happen. One is that the overall navigation graph gets quite complex. The other is that you have to duplicate core responsibilities, in a few different ways.

The diagram below is an approximation — and subset — of the core Activities (purple) that we had. There were various entry points (green)into the app, and also inherently multiple interactions with Android’s lifecycle system.

Unsurprisingly, this navigation arrangement is quite difficult to understand, both in design and implementation. Let’s suppose a user starts the app. The set of app services isn’t fully set up yet so it shows the splash screen whilst that happens. The splash screen finishes and should transition to the main page, but we then want to prompt the user to sign in, so they get directed to the sign in page, which they dismiss, so we go back to the main page, which has to make sure it doesn’t show sign-in again this time. Clear?

This also implicitly tells you about the duplication of responsibility. You could in theory leave and return (resume) to any of these activities, and the app could have been torn down in the interim, so do they all need to worry about whether the app is started yet, and hand the user to the splash screen? Do they all need to worry about signed-in status?

Wouldn’t it be better to give them a sort of scope, so they only exist under the relevant circumstances and only have to worry about doing their own job?

The Collective Noun For Fragments: A Hierarchy

When we think about some of these navigation decisions, we can often come up with a set of behaviours that involve exclusivity but perhaps aren’t immediately obvious. These are some of ours:

  • if the core app services aren’t finished initialising yet, the user should only be able to see the splash screen
  • if the app has been disabled by remote config (e.g. it needs the user to go and upgrade it in the store), the user shouldn’t be able to access any other UI besides the message
  • if the app is enabled, we might ask the user to sign in
  • if we do ask them to sign in, it shouldn’t matter what content they’re looking at, the sign in page should appear on top

From this you can start to derive some ideas.

Depending on the app’s initialisation status, you can have a splash screen OR a ‘disabled’ screen OR the app is enabled. If the app is enabled it will show sign-in OR some actual content.

We can build a hierarchy around this.

We set out with a top level fragment that’s always present, MainFragment. It (or something it owns) decides which kind of child to show as its content, and that decision is based on the initialisation status, aka ‘bootstrap state’.

If we are initialised and enabled, we bring the first content layer into play — AppEnabledFragment in this diagram. It too is responsible for decision making, but at a lower level: whether to either show sign-in, or a further layer of content.

Properly Defined Responsibilities

This probably already seems like a vaguely sensible thing to do, but the benefit is worth calling out explicitly.

AppEnabledFragment only operates in a world where the app is enabled. It doesn’t care or even know about splash screens or showing upgrade messages because this is outside of its scope, and it either doesn’t exist or isn’t active in those circumstances.

Conversely, the MainFragment doesn’t care about any of the detail of how content is displayed, only that it has been put into motion or taken out of service. Decisions are made once in one place.

Single Activity

In the above system, there is one activity. One activity means one set of interactions with the lifecycle — you leave the app and return and it’s one overarching activity that gets onPause and onResume calls. We can re-evaluate the layers of decisions we made at this point.

If for example the application has been cleaned up, we will recreate the UIs from the ground up, and not have to worry about handling an ‘uninitialised’ state in the content components.

Similarly, if we get a deep link intent, then from an activity perspective, we handle it ‘in house’ rather than having to work out how to summon the right activity without a conventional step-by-step user journey behind it. This yields consistent user journeys.

Jetpack Navigation?

If you’re an Android dev, you might be looking at this and thinking ‘what about Jetpack Navigation?’. Well, if you look at what JN prescribes, the above and particularly the single activity model is one of the most important elements, for exactly the reasons already given. JN in turn is effectively becoming the intended model for modern Android navigation.

We aren’t yet using JN, but we might in future. What we’ve done takes us much closer to compatibility with it. What JN would really give us from this point onwards is a set of design tools and frameworks that replace the detailed fragment transactions we use to power it.

You might think that to rework our navigation but not adopt JN now is a missed opportunity. That’s possibly true, but the fundamentals of this refactor work already put enough technical risk in flight. There are some big, overarching transformations here that are of a larger size and scope than we would normally be comfortable with. It's only because we have experience in these areas that this becomes acceptable. For us, we don't have that with JN yet and we'd rather do one thing at a time, controlling our exposure to risk as much as possible. It is absolutely still on the cards to investigate.

Up Next

In the next article in this series, we’ll look at how we structured the fragments themselves; primarily what presentation design pattern we decided upon, but also what to do about the frameworks that might make it happen.

Modernising A Legacy Android App Architecture, Part One: The Single Activity was originally published in BBC Design + Engineering on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source: BBC