Your solution is good, but it is not safe, it will anyways spawn a new Thread
when you call start
and that thread
will sleep
all good so far but you need to call navController
from UI thread, also if user quits the app before 500
milliseconds it will crash.
Here is a solution Kotlin coroutines
way
val mDelayJob: CompletableJob = Job()
//use mDelayJob to cancel coroutine in onStop, so if even user exits before delay, nothing will happen
fun splash(){
CoroutineScope(Dispatchers.Main).launch(mDelayJob){
delay(500)
findNavController().navigate(R.id.action_splahFragment_to_loginFragment)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…