Why It Is Preferable to Use “Single Activity Architecture” Over Multiple Activity Architecture in Android App Development?

As a developer, we are working in the field of Android application development for a long time and Activity is the most familiar thing to us. It is the first component among the other major components that we have used earlier in the field of Android, hence the familiarity.

Now, the word “Activity” may not sound familiar to most people, as we are much concerned about the software that we will use in our smartphone than knowing its dynamic behaviours and functionalities in the background.

We, often use the word “page” when we use any Android application, for example, “After filling up the details please, click on the ‘Next’ button’ and go to the next page in the application”. The word page is nothing but technically referring to “Activity”. As the complexity of the application increases, the number of Activities also increases.

In 2018, Google first announces the release of the Jetpack library at their Annual Developers Conference. Along with the Jetpack, they had also spoken about the term “Single Activity Architecture” and it seems similar architecture will be preferred in future.

What is Single Activity Architecture?

It is an architecture that must consist of one Activity or in some cases relatively small number of activities. So, we need to know how this architecture works.

Earlier when we use to declare multiple activities in our projects, where each one of them was used to represent on front-end or UI (User Interface). Instead, in this architecture, we define an Activity as a container and multiple Fragments inside that Activity will represent the User Interface.

Drawbacks of Multiple Activity Architecture

  1. Earlier when we were completely dependent on this architecture we have to perform lots of manual coding and navigation flow, declare the Activities in the Manifest file. Also if we want to communicate from one activity to another, we have to use methods like startActivityForResult() to send a request to another activity. This entire process is very tedious and time-consuming. Multiple Activity Architecture
  2. It will also affect the animation when we transit from one Activity to another Activity. More specifically we can spot some blinking in the status bar/action bar during the transition.
  3. Maximum times, whenever we want to share any data between two activities, we use a Singleton Class (A concept of Java class where we can create only one instance of it). However, the data can be accessed by the Services and Content Providers as it will be in the Application Scope. Singleton Class

Advantages of Using Single Activity Architecture

  1. From the name only we can understand that only one Activity will exist in the whole architecture. So, no need to update the Manifest every time. Just once, we have to declare the Navigation graph XML file in the Manifest.
  2.  No need to declare the boilerplate method like startActivityForResult() every time as we will navigate between the fragments now and the navigation between the fragments get also simplified with the use of the Navigation Component library included in JetPack. One Activity
  3. The transition Animation problem has also been resolved after using the fragments.
  4. As all the Fragments will be bounded inside the Activity, we can easily share the data between the fragments. single activity
  5. We might counter incidents like we need to pass information to the fragments. It may consist of nullable data. Manually, if we have to resolve this problem then we have to write several lines of code. But in Navigation Component, Android introduced a feature called “Safe Args Gradle Plug-in”. Builder classes will be generated from this plug-in to assure type-safe access to the arguments for the particular action.

As we have already discussed the benefits, it’s our suggestion to the future Android Developer to switch into Single Activity Architecture using Navigation Components.

Android App Development

For example, we are working on an authentication module in any application. Instead of using multiple activities for login and registration, we can simply approach this architecture by creating fragments for every module for authentication and then bound them in a Single activity. It also provides the mobility to switch between the fragments and increase the stability of the application.

Source: https://android-developers.googleblog.com/2018/05/use-android-jetpack-to-accelerate-your.html

Sankhadeep Ghosh

Sankhadeep, a faculty at Webskitters Academy, have years of experience in the professional IT industry. Having worked as a professional, and gaining thorough and detailed understanding of the industry, he decided to share his experience in training young aspirants. With his talents, and expertise, he is able to resolve the queries of the students and polish their skills to perfection.

Call Request Form