import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_screen)
val button = findViewById<Button>(R.id.goNextButton)
button.setOnClickListener {
val intent = Intent(this, SecondScreen::class.java)
startActivity(intent)
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
}
override fun onBackPressed() {
super.onBackPressed()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
}
class SecondScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second_screen)
val backButton = findViewById<Button>(R.id.backButton)
backButton.setOnClickListener {
finish()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
}
override fun onBackPressed() {
super.onBackPressed()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
}We added a button click listener in MainScreen that starts SecondScreen. Immediately after starting the activity, overridePendingTransition is called with Android's built-in fade in and fade out animations to create a smooth fade effect.
In SecondScreen, a back button finishes the activity and calls overridePendingTransition again to reverse the fade animation. We also override onBackPressed in both screens to ensure the fade animation plays when the user presses the device back button.
This creates a simple and clean transition animation between the two screens.