Android one activity to another activity, Fragment to Activity - Display Toast - kotlin

 First, create a button with id button1 in the XML file. Then write the code in onCreate in the Kotlin file.

This will go from MainActivity to MainActivity2.



Activity to Activity.

val button1 = findViewById<Button>(R.id.button1)
button1.setOnClickListener {
Toast.makeText(this@MainActivity, "Button 1 Clicked", Toast.LENGTH_SHORT).show()
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}



Fragment TO Activity

val button = view.findViewById<Button>(R.id.button)
button.setOnClickListener {
Toast.makeText(requireContext(), "Button Clicked", Toast.LENGTH_SHORT).show()
val intent = Intent(requireContext(), ListViewActivity::class.java)
startActivity(intent)
}

Comments