Alternatives to AsyncTask (deprecated) in Android development?

Discover effective alternatives to AsyncTask in Android development, including Handler, Coroutine, and RxJava, to handle background tasks efficiently and avoid performance issues.
Android development, AsyncTask alternatives, Handler, Coroutines, RxJava, background tasks

    // Example of using Kotlin Coroutines as an alternative to AsyncTask
    
    import kotlinx.coroutines.CoroutineScope
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.launch
    import kotlinx.coroutines.withContext

    fun fetchData() {
        CoroutineScope(Dispatchers.Main).launch {
            val data = withContext(Dispatchers.IO) {
                // Simulate network call
                Thread.sleep(1000) // Replace with actual network call
                "Result from background task"
            }
            // Update UI with the result
            updateUI(data)
        }
    }

    fun updateUI(data: String) {
        // Update your UI components here
        println(data)
    }
    

Android development AsyncTask alternatives Handler Coroutines RxJava background tasks