I have created two activites MainActivity and Main2Activity. I want to launch Main2activity from MainActivity and also want to finish current activity and show a welcome toast. I am using this code
val intent = Intent(this,Main2Activity::class.java)
Toast.makeText(this,getString(R.string.welcome),Toast.LENGTH_LONG).show()
finish()
startActivity(intent)
so i have no problem but i need help when i run this code it works fine but Main2Activity take long to open and toast not shown for long when Main2Activity opens toast disappears in a few seconds so i think i have arrange the code wrong some one tell me how to arrange this code correctly. for example: finish first,toast second and then start activity.
It's better to do not leave a toast message with a finished activity. It may cause some problems like Screen Overlay Detected error. So, do this:
In MainActivity:
val intent = Intent(this, Main2Activity::class.java)
intent.putExtra("SHOW_WELCOME", true)
startActivity(intent)
finish()
In Main2Activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.getBooleanExtra("SHOW_WELCOME", false)) {
Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_LONG).show()
}
}