Answer the question
In order to leave comments, you need to log in
How to change fragment (android) after 3 seconds?
Hello!
My application consists of 3 main screens (fragments that switch between themselves bottom navigation).
When the application starts, the Home screen opens (shown in the code) The
question is the following. I want to add a welcome snippet that will be the very first to run, where I'll make an animation and write something like "Hi, I'm an application!".
How can I make this fragment run first, and most importantly, how can I set it a time, for example 3 seconds, after which the Home fragment will start (that is, it will go to the fragment that I have right now when I start the application)?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
replaceFragment(HomeFragment())
binding.bottomNavigationView.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.home -> {
replaceFragment(HomeFragment())
true
}
R.id.profile -> {
replaceFragment(ProfileFragment())
true
}
R.id.settings -> {
replaceFragment(SettingsFragment())
true
}
else -> false
}
}
}
private fun replaceFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout, fragment)
transaction.commit()
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question