A
A
Alexander Koshelev2022-03-08 08:42:55
Android
Alexander Koshelev, 2022-03-08 08:42:55

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

1 answer(s)
O
Oleg, 2022-03-08
@Xandr24

you're doing bullshit.
Basically you want a splash screen. But through a fragment, you will not be able to achieve a decrease in the cold start time of the application.
Read how everyone else is doing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question