How to Write Good Code in Android using Starter Pattern?
It is not difficult to write code in Android. All you have to do is read a few articles or tutorials and create some code for an Android app. But it’s the quality of code you produce for the same application that sets you apart from other Android developers. A decent Android code not only sets you apart from other developers but also helps you prevent some of the Runtime issues that you or Android might miss at build time. So, in this article, we’ll learn how to use the Starter Pattern to build good Android code.
What exactly is the issue?
Before we look at the answer, we must first understand the problem. Let’s look at an example to better comprehend the situation. We have one PostDetailActivity in our example that contains the details of a post, such as a title and a picture URL. Also, bypassing the title and picture URL, this PostDetailActivity is started from two activities, namely MainActivity and PostListActivity. We have a constant class as well, which is used to declare constant values. As a result, the constant class’s code is as follows:
The PostDetailsActivity’s code is as follows:
The MainActivity’s code is as follows:
So, is the code working as it should at this point? It ought to operate nicely. However, while working in a collaborative atmosphere, where multiple developers are working on the same project, issues occur. As an example, suppose you need to add a thumbnail image to your application at some point. So, in the Constant class, you’ll simply add one const value:
Because you aren’t the only one working on this project. Some other developers may have included the thumbnail link in the MainActivity but not in the PostListActivity. There will never be a compile-time error here. However, you will find some bugs when using the program, notably the PostListActivity, and these should be addressed solely during the development phase. In addition, if you’re working on a large project, these constants may be larger, and you’ll need to include each one in the activity that calls the PostDetailActivity.
val articleImageThumbnails = intent.getStringExtra(Constants.POST_TITLE)
So, we’ve talked about a problem that might arise with any developer. You may discover bugs at runtime if you do not write the correct code (syntax-wise).
The code in Starter Pattern can be used to solve the problem mentioned above.
We use several static methods in the Starter Pattern, to begin with, the values that will be used in our activity. We can define the needed values of a class in the same class instead of creating a separate constant class for the constant values. As a result, you can only access those variables from that class whenever you need them. We add the extras values in the getStartIntent() function and get the value in the onCreate() function in the above code ().
Using the code below, you can now call the PostDetailActivity from the MainActivity:
We’re going to add one more field here, which is the thumbnail URL.
You will get a compile-time error if you forget to give the thumbnail URL from any activity using PostDetailActivity in this scenario. You may then add or pass the required variable. The MainActivity example is as follows:
Start using the Starter Pattern if you want to produce good Android code and stand out from the crowd. The Starter Pattern is used to construct code in such a way that the variables are all started in the same Activity. This will reduce runtime errors and aid in the detection of any mistakes that may occur while working on a collective project.