Member-only story
Learning by Example
Kotlin: Sealed Classes
A concrete example of when you might use sealed classes in Android

There have already been quite a few articles written on the topic of Kotlin sealed classes and so I am not going to rehash here what I believe others to have already adequately covered. Instead this article will demonstrate a practical example of utilizing sealed classes in Android code so that you can get a better idea of how and when you might leverage them.
In our example we will build off of the Creating an Android App Launcher article that I wrote last month. In that article I showed how one might go about creating a feature within their app for launching other, third-party apps. If you haven’t already checked that article out then you may want to jump over and give it a quick read. If you did already read the article and also checked out the accompanying GitHub sample project then you may have noticed that inside of the AppLauncherViewModel class there was an inner, sealed class named App and one concrete subclass of App named AppInfo. Here they are again for the sake of convenience: