Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Created March 18, 2020 11:41
Show Gist options
  • Save pfieffer/e3094cc7a1992b72a32abd73ad04db24 to your computer and use it in GitHub Desktop.
Save pfieffer/e3094cc7a1992b72a32abd73ad04db24 to your computer and use it in GitHub Desktop.
A Splash Screen based on Kotlin Co routine which can be cancelled by the user and it would not launch the HomeActivity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.coroutines.*
class CoroutinesSplashActivity : AppCompatActivity() {
val activityScope = CoroutineScope(Dispatchers.Main)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_coroutines_splash)
activityScope.launch {
delay(3000)
var intent = Intent(this@CoroutinesSplashActivity, HomeActivity::class.java)
startActivity(intent)
finish()
}
}
override fun onPause() {
activityScope.cancel()
super.onPause()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment