Create standalone Android apps
When I started developing android apps back in 2017, I remember creating activities for every single screen or feature in the app and baking all the business logic inside those activities. The code looked very complicated and messy and I will proudly show it to my friends. I was in 11th grade at about 15-16, and I thought I was so smart, “Nobody can do what I do,” I said to myself.
Then I learned about the fatheragments and there was nothing I hated more about android development. what is the point? They just seemed like activities but they made everything more difficult. I wasn’t able to get hash transactions or pass some data between multiple fragments without going through a maze of errors (which taught me a lot by the way). To spare me the hassle, I only used fragments when necessary and continued to use activities across entire applications.
In my quest to further develop skills and learning, I’ve discovered a clean architecture and SOLID principles that I try to apply in every software I create, mobile or otherwise. The next app I made had 1 activity and about 4 parts. Turns out the shards weren’t too bad after all. I had a much better appreciation for splinters at this point and vowed to always use them. I even picked up a jetpack compose. Long story short, I hate splinters again.
If you have never used a jetpack before you have given it a try. I won’t go into any details about how it works or the basics. The best resources I used were coding with mitch and Google documentation. In this article, I go over one of the many ways to build single activity apps using jetpack libraries.
The basic idea is to have a single activity that acts as the main entrance and exit for your app. Some applications may require the creation of several activities, for example, a file
SettingsActivity But the basic idea remains the same.
Before we begin, it is important to understand the main components that make up most applications, in this context, it is basically one or more screens that the user interacts with and a mechanism for moving between these screens if there is more than one. How does this work?
Once our basic activity is ready, we now need to set up our navigation console which will allow us to move between different screens. In this example, we are assuming that our app has only 3 enabled screens, namely Home, Search and Library.
The next step is to create a high-level Singleton screen object that maps unique paths to all possible screens in your app. For example, this object – stored in a top-level Kotlin file – looks something like this:
MainActivity We are now adding
NavHost With the initial path set to
Screen.Home.route Because this is the default screen of our app.
The next step is to put all top-level destinations – in our case Home, Search, and Library destinations – inside NavHost. To do this, we make use of the Kotlin extension functionality and express each of these screens as extensions of
We make use of the Kotlin extension functionality and express each of these screens as extensions to NavGraphBuilder.
MainActivity We now put it inside
NavHost As follows:
Of course, the app will not work that way. To actually navigate between screens, you’ll need a scaffold with a bottom app bar or some other navigation, for example, click on the button that calls
navController.navigate(route). The above functions allow you to create a navigation graph that defines the relationships that exist between the different screens in your app.