After rebuilding my project, I get an error saying that one of my methods is too large, and it then directs me to a click listener. Does anyone know a good way of splitting the method or breaking it down? I honestly I have no idea on the recommended way to do this when all the connected strings (500+ within an RecyclerView and array) are related to the same item.
Method too large
and
e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation
ibMap.setOnClickListener {
when {
tvTitle.text.toString() == "029" -> {
when {
isAppInstalled -> { // Intent to launch Google Maps if the standard package is already installed
val gmmIntentUri =
Uri.parse("geo:0,0?q=Cardiff, United Kingdom")
val mapIntent =
Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.maps")
// Attempt to start an activity that can handle the Intent
mCtx.startActivity(mapIntent)
}
isLiteAppInstalled -> { // Intent to launch Google Maps Go if the lite package is already installed
val gmmIntentUri =
Uri.parse("geo:0,0?q=Cardiff, United Kingdom")
val mapIntent =
Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.mapslite")
mCtx.startActivity(mapIntent)
}
else -> { // Intent to launch Google Maps in web browser if neither of the above apps are installed
val str =
"https://www.google.com/maps/place/Cardiff,+United+Kingdom/"
// Attempt to start an activity that can handle the Intent
mCtx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(str)))
}
}
}
// 500 more strings
}
You have a lot of duplicate code even in this small sample. In general you should avoid copy pasting code blocks and instead extract them as separate method. First add it inside your RecyclerView adapter or wherever your onclick can reach:
fun launchMapIntent(locationName: String) {
// determine package to open
val mapPkg = when {
isAppInstalled -> "com.google.android.apps.maps"
isLiteAppInstalled -> "com.google.android.apps.mapslite"
else -> null
}
val mapIntent = if(mapPkg != null) {
// open app
val gmmIntentUri = Uri.parse("geo:0,0?q=$locationName")
Intent(Intent.ACTION_VIEW, gmmIntentUri).setPackage(mapPkg)
} else {
// fallback to browser
val encLoc = Uri.encode(locationName)
val str = "https://www.google.com/maps/place/$encLoc/"
Intent(Intent.ACTION_VIEW, Uri.parse(str))
}
mCtx.startActivity(mapIntent)
}
Then you can clean up duplicate code from your onclick listener:
ibMap.setOnClickListener {
when(tvTitle.text.toString()) {
"029" -> launchMapIntent("Cardiff, United Kingdom")
"030" -> launchMapIntent("other location...")
// other cases
}
}
Alternatively if all of your cases are opening the maps:
ibMap.setOnClickListener {
val locationName = when(tvTitle.text.toString()) {
"029" -> "Cardiff, United Kingdom"
"030" -> "other location..."
// other cases
}
launchMapIntent(locationName)
}
You could further optimize by making a Map<Int, String>
with numbers as keys and location names as values instead of having a huge switch or somehow hold them in a database etc