override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityGamePlayBinding.inflate(layoutInflater)
setContentView(binding.root)
val btn1Hide = findViewById<Button>(R.id.btn2Choice)
val btn2Hide = findViewById<Button>(R.id.btn2Choice2)
val btn3Hide = findViewById<Button>(R.id.btn2Choice3)
val btnAcceptHide = findViewById<Button>(R.id.btn2Accept)
val InputHide = findViewById<TextView>(R.id.textInputName)
val page = intent.getStringExtra(PageName.page).toString()
when (page) {
"2" -> {
btn1Hide.visibility = View.GONE
btn2Hide.visibility = View.GONE
btn3Hide.visibility = View.GONE
binding.text2Edit.text = getString(R.string.p2_hello)
}
"3" -> {
InputHide.visibility = View.GONE
btnAcceptHide.visibility = View.GONE
val name = intent.getStringExtra(PageName.name).toString()
binding.text2Edit.text = getString(R.string.p3_great, name)
}
}
binding.btn2Accept.setOnClickListener {
when (page) {
"2" -> {
if (binding.textInputName.text != null) {
val intent = Intent(this, GamePlay::class.java)
intent.putExtra(PageName.name, binding.textInputName.text.toString())
intent.putExtra(PageName.page, "3")
startActivity(intent)
finish()
}
}
}
}
binding.btn2Choice.setOnClickListener {
val intent = Intent(this, ScenarioPlay::class.java)
intent.putExtra(PageName.page, "4")
startActivity(intent)
finish()
}
binding.btn2Choice2.setOnClickListener {
val intent = Intent(this, ScenarioPlay::class.java)
intent.putExtra(PageName.page, "5")
startActivity(intent)
finish()
}
binding.btn2Choice3.setOnClickListener {
val intent = Intent(this, ScenarioPlay::class.java)
intent.putExtra(PageName.page, "6")
startActivity(intent)
finish()
}
}
Within that code snippet, there are several different logics that are executed sequentially in the onCreate method. This includes setting the visibility of the button, setting the text in binding.text2Edit, logic based on the page value to set the visibility and text, and adding click functionality on the buttons to intent to the ScenarioPlay class.
Lengthy onCreate methods like this can make it difficult to understand the code and maintain it afterwards. It is recommended to separate the different logic into separate methods to increase the clarity and maintainability of the code.
The onCreate method in the code has a lot of logic related to setting the view based on the page value. Each when block checks the page value and configures the view accordingly.
Long Method occurs when a method becomes too long and complex. In this case, the onCreate method has many different when blocks that adjust the view by page. Because this logic is tied to view settings, extending this method makes it difficult to understand and maintain.
Example 1
From
GamePlay.kt
fileWithin that code snippet, there are several different logics that are executed sequentially in the onCreate method. This includes setting the visibility of the button, setting the text in binding.text2Edit, logic based on the page value to set the visibility and text, and adding click functionality on the buttons to intent to the ScenarioPlay class.
Lengthy onCreate methods like this can make it difficult to understand the code and maintain it afterwards. It is recommended to separate the different logic into separate methods to increase the clarity and maintainability of the code.
Example 2
From
ScenarioPlay.kt
fileThe onCreate method in the code has a lot of logic related to setting the view based on the page value. Each when block checks the page value and configures the view accordingly.
Long Method occurs when a method becomes too long and complex. In this case, the onCreate method has many different when blocks that adjust the view by page. Because this logic is tied to view settings, extending this method makes it difficult to understand and maintain.