title: What is the proper way to launch a coroutine on Android?
body: <p>I'm trying to figure out how to launch a coroutine.  I want it to call two suspend functions in sequence.</p>
  
  <p>The first docs I read said to do this:</p>
  
  <pre><code>class Something {
    fun initialize() {
      launch {
        suspendFun1()
        suspendFun2()
      }
  }
  
  </code></pre>
  
  <p>But the <code>launch</code> method was not found by Android Studio.  Then I learned that the <a href="https://kotlinlang.org/docs/reference/coroutines/basics.html" rel="noreferrer">official coroutine docs</a> suggest using <code>GlobalScope.launch</code>:</p>
  
  <pre><code>class Something {
    fun initialize() {
      GlobalScope.launch {
        suspendFun1()
        suspendFun2()
      }
  }
  </code></pre>
  
  <p>But then I read in <a href="https://stackoverflow.com/questions/54335365/why-not-use-globalscope-launch">this post</a> that you should not use <code>GlobalScope.launch</code>.  </p>
  
  <p>So I found another <a href="https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd" rel="noreferrer">blog post</a> explaining that I need a CoroutineScope to call <code>launch</code>.  But it doesn't explain how to build one.</p>
  
  <p>Then I found this <a href="https://proandroiddev.com/coroutines-for-android-6f9b9f966056" rel="noreferrer">blog post</a> explaining how to build one for Activities and ViewModels, by implementing CoroutineScope on the class:</p>
  
  <pre><code>class Something : CoroutineScope {
    private lateinit var job: Job
    override val coroutineContext: CoroutineContext
          get() = job + Dispatchers.Main
  
    fun initialize() {
      job = Job()
      launch {
        suspendFun1()
        suspendFun2()
      }
  }
  </code></pre>
  
  <p>Then I read this <a href="https://proandroiddev.com/why-your-class-probably-shouldnt-implement-coroutinescope-eb34f722e510" rel="noreferrer">blog post</a> saying I should not implement <code>CoroutineScope</code></p>
  
  <pre><code>class Something {
    protected val scope = CoroutineScope(
      Job() + Dispatchers.Main
    )
  
    fun initialize() {
      scope.launch {
        suspendFun1()
        suspendFun2()
      }
  }
  </code></pre>
  
  <p>But I'm not sure I understand the meaning of <code>Job() + Dispatchers.Main</code>, as this seems to work as well:</p>
  
  <pre><code>class Something {
    protected val scope = CoroutineScope(Dispatchers.Main)
  
    fun initialize() {
      scope.launch {
        suspendFun1()
        suspendFun2()
      }
  }
  </code></pre>
  
  <p>Can someone explain to me simply the best way to do this?  Is it as above?  I'm sure this has been asked already, so I apologize if this is a duplicate question.  But as you can see, there are not clear answers on this.  I would like to hear your input.</p>