Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Write Good Code in Android using Starter Pattern?

  • Last Updated : 26 Nov, 2021

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:

Kotlin




object GeeksforGeeksConstants {
    internal const val POST_TITLE = "android_post"
    internal const val POST_URL = "gfg_url"
}

The PostDetailsActivity’s code is as follows:

Kotlin




class AndroidArticle : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.article_title)
        val articleTitle = intent.getStringExtra(Constants.POST_TITLE)
        val articleImage = intent.getStringExtra(Constants.POST_IMAGE_URL)
    }
}

The MainActivity’s code is as follows:

Kotlin




class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val intent = Intent(this@MainActivity, ArticleActivity::class.java)
        intent.putExtra(Constants.ARTICLE_TITLE, "android")
        intent.putExtra(Constants.POST_IMAGE_URL, "gfg")
        startActivity(intent)
    }
}

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:

Kotlin




object GfGConstants {
    internal const val POST_TITLE = "android_post"
    internal const val POST_IMAGE_URL = "image_url"
    internal const val POST_THUMBNAIL = "thumbnail"
}

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)

The Solution

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.

Kotlin




class ArticleDetails : AppCompatActivity() {
    companion object {
        // all the constants values
        internal const val ARTICLE_TITLE = "ARTICLE_TITLE"
        internal const val POST_IMAGE_URL = "post_image_url"
        // starter function
        fun getStartIntent(context: Context, POST_TITLE: String, postImageUrl: String): Intent {
            val intent = Intent(context, ArticleDetails::class.java)
            intent.putExtra(ARTICLE_TITLE, POST_TITLE)
            intent.putExtra(POST_IMAGE_URL, postImageUrl)
            return intent
        }
    }
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_post_detail)
        val POST_TITLE = intent.getStringExtra(ARTICLE_TITLE)
        val postImageUrl = intent.getStringExtra(POST_IMAGE_URL)
    }
}

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:

Kotlin




class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val intent = PostDetailActivity.getStartIntent(this@PostActivity, "android", "geeksforgeeks")
        startActivity(intent)
    }
      
}

Kotlin




class PostDetailActivity : AppCompatActivity() {
  
    partner object {
        // constants
        internal const val POST_TITLE = "post_title"
        internal const val POST_URL = "POST_URL"
        internal const val POST_THUMBNAILS = "post_image_thumbnail_url"
  
        // starter function
        fun getStartIntent(context: Context, gfgPostTitle: String, postImageUrl: String, postImageThumbailUrl: String):Intent {
            val intent = Intent(context, PostDetailActivity::class.java)
            intent.putExtra(POST_TITLE, gfgPostTitle)
            intent.putExtra(POST_URL, postImageUrl)
            intent.putExtra(POST_THUMBNAILS, postImageThumbailUrl)
            return intent
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_post_detail)
        val gfgPostTitle = intent.getStringExtra(POST_TITLE)
        val postImageUrl = intent.getStringExtra(POST_URL)
        val postImageThumbnailUrl = intent.getStringExtra(POST_THUMBNAILS)
    }
}

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:

Kotlin




class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val gfgIntent = PostDetailActivity.getStartIntent(this@MainActivity, "android", "postImageUrl", "imageThumbnail")
        startActivity(gfgIntent)
    }
}

Conclusion

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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!