提问者:小点点

我如何在几秒钟内在KOTLIN的android工作室中自动移动一个角色到另一个角色[重复]


我不能在KOTLIN(android studio)中几秒钟内自动将一个活动移动到另一个活动。你能回答吗我怎么能这样移动

自动调用移动在kotlin android工作室


共1个答案

匿名用户

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 !

            } 
        }

如果您不需要活动,请使用片段并使用片段导航

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.

        }
    }
   }