Search code examples
androidkotlinasynchronousintellij-idea

Asynchronous programming in IntelliJ vs Android Studio


I like to test programming concepts using these 2 IDEs, however, I cannot help to notice that the Main function in IntelliJ has some differences compared to the OnCreate function in Android.

IntelliJ

fun main(args: Array<String>) {

val flow = flowOf(1, 2, 3)
println("Before launch")

CoroutineScope(Dispatchers.IO).launch {
    println("Launch working")
    flow.collect {
        println("Collect: $it")
    }
}
}

Results

Before launch

This is understandable as I need to use the join on the job object in order to get the coroutine to wait for the Child coroutine to be finished. The code below works fine.

Something like this

fun main(args: Array<String>) = runBlocking {

val flow = flowOf(1, 2, 3)
println("Before launch")

var job = CoroutineScope(Dispatchers.IO).launch {
    println("Launch working")
    flow.collect {
        println("Collect: $it")
    }
}
job.join()}

However in the Android Studio OnCreate function I do not need to use the join method to wait for the coroutine to finish.

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
...
lifecycleScope.launch(Dispatchers.Main) {
    requestNews(GENERAL, generalNews, "us")
    requestNews(TECHNOLOGY, TechNews, "us")
    requestNews(HEALTH, healthNews, "us")
    requestNews(SPORTS, SportsNews, "us")
    requestNews(ENTERTAINMENT, EntertainmentNews, "us")
    requestNews(SCIENCE, ScienceNews, "us")
    requestNews(BUSINESS, BusinessNews, "us")
}

}}

The above code in Android Studio OnCreate function works,all of the requests were initiated. I did not have to use job.join to wait for the coroutine. Is there a reason why there is a difference in the way it works?

IntelliJ requires the need of using the job.join function to execute child coroutines properly while OnCreate doesn't, am I missing something?


Solution

  • You're conflating IDEs and target platforms in your question description.

    • IntelliJ is not a type of app. It's an IDE for writing apps of all kinds.

    • You can build Android apps in IntelliJ IDEA.

    • You can build JVM apps in Android Studio (although it doesn't provide a new project wizard for creating a project specifically for that).

    So your question is really about the difference between a JVM app and an Android app.

    A JVM app works by executing the main function, and it terminates once the main function returns. So, if you start some asynchronous work without waiting for it, then the app will be terminated before the asynchronous work finishes.

    An Android app works by looking at the app's manifest for the Activity associated with the launcher. Then it constructs the associated Activity subclass and sets up its Context and other dependencies, and then starts its lifecycle, which includes the onCreate() function. The Activity is not terminated until some user action like backing out of it or rotating the screen, or you calling finish(). The Application process (which was hosting the Activity) is not terminated until the OS decides to, or the user force closes it through the Android settings.

    This is a simplification of how Android works. An app doesn't necessarily even need to have any launcher activity. It could be just a service. And the launcher activity is not the only way the application might be started.