我如何自动移动一个活动到另一个在几秒钟在android工作室KOTLIN



我不能自动移动一个活动到另一个活动在KOTLIN (android工作室)几秒钟。你能回答我这个问题吗?

在kotlin android studio中自动移动

https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope

lifeCyclerScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.CREATED) {
launch {
delay(5000) //milisec
//after 5 sec delay do it !
} 
}

如果你不需要一个activity,使用fragment和fragment导航

https://developer.android.com/guide/navigation/navigation-getting-started

生命周期镜和导航示例

class MyFragment: Fragment {
init { // Notice that we can safely launch in the constructor of the Fragment.
lifecycleScope.launch {
whenStarted {
// The block inside will run only when Lifecycle is at least STARTED.
// It will start executing when fragment is started and
// can call other suspend methods.
loadingView.visibility = View.VISIBLE
val canAccess = withContext(Dispatchers.IO) {
checkUserAccess()
}
// When checkUserAccess returns, the next line is automatically
// suspended if the Lifecycle is not *at least* STARTED.
// We could safely run fragment transactions because we know the
// code won't run unless the lifecycle is at least STARTED.
loadingView.visibility = View.GONE
if (canAccess == false) {
findNavController().popBackStack()
} else {
showContent()
}
}
// This line runs only after the whenStarted block above has completed.
}
}
}

最新更新