October 2019
Intermediate to advanced
434 pages
11h 54m
English
A common pattern for the creation of new activities or fragments is to create static factory methods to start an activity, or to create an intent, or to create a new instance of Fragment. When using Kotlin, we can accomplish this using a companion object, which is shown in the following code block with the definition of Factory:
companion object Factory { const val EXTRA_ID = "extra_id" @JvmStatic fun createIntent(context: Context, id: String) = Intent(context, DetailsActivity::class.java).apply { putExtra(EXTRA_ID, id) }}
Additionally, you could use a top-level function or extension function to achieve similar behavior, as follows:
fun createDetailsIntent(context: Context, id: String) ...
Read now
Unlock full access