Search code examples
androidkotlinandroid-progressbar

Splash screen with horizontal progressBar with using coroutines


I am trying to create splash screen with progressBar. I want create it with using coroutines. Here is my code:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.splash_screen)
        progressBarLoading = findViewById(R.id.loadingProgress)
        lifecycleScope.launch {
            delay(1000)
            if (progressBarStatus < 100) {
                progressBarLoading?.setProgress(progressBarStatus)
                progressBarStatus = progressBarStatus + 30

            } else {
                var intent = Intent(this@SplashActivity, MainActivity::class.java)
                startActivity(intent)
            }
        }
    }

but when splash screen is showing , progress Bar doesn't move and absent switching to the second screen. I can't understand what is wrong.


Solution

  • You are just calling your code once . you need to do something like

    lifecycleScope.launch {
                while(true){
                    delay(1000)
    
                    if (progressBarStatus < 100) {
                        progressBarLoading?.setProgress(progressBarStatus)
                        progressBarStatus += 30
    
                    } else {
                        var intent = Intent(this@SplashActivity, MainActivity::class.java)
                        startActivity(intent)
                        finish()
                        break
                    }
                }
            }