title: Code block in coroutine using "Dispatchers.Main" not being run
body: <p>I've searched everywhere and I haven't found anything that seems to be a solution to my problem</p>
  
  <p>I have a function using coroutines:</p>
  
  <pre><code>fun onAuthenticated() {
      launch (Dispatchers.IO) {
          userRepo.retrieveSelf()!!.let { name -&gt;
              userRepo.addAuthenticatedAccount(name)
              userRepo.setCurrentAccount(name)
          }
  
          activity?.setResult(Activity.RESULT_OK, Intent())
  
          // this block doesn't seem to be run
          withContext(Dispatchers.Main) {
              Log.d(TAG, "ok looks gucci")
              activity?.finish()
          }
      }
  }
  </code></pre>
  
  <p>When this function is called, the code in the <code>withContext(Dispatchers.Main) { ... }</code> block doesn't run. I'm using it to access the activity in the main thread.</p>
  
  <p>I've been getting kind of frustrated and I'm not sure if I don't understand how the dispatcher/coroutine is supposed to work or there's something I'm missing.</p>
  
  <p>Let me know if you need any additional details or code!</p>
  
  <p><strong><em>EDIT</em></strong>
  So Marko was right. After I moved the <code>activity.?.setResult(Activity.RESULT_OK, Intent())</code> so that it was being run with the main dispatcher, I found out there was another part of the code in <code>userRepo.setCurrentAccount(name)</code> that was giving an issue. After updating the code as seen below, it works as expected!</p>
  
  <pre><code>override fun onAuthenticated() {
      val handler = CoroutineExceptionHandler { _, e -&gt;
          Snackbar.make(
              web_auth_rootview,
                  "Authentication unsuccessful",
                  Snackbar.LENGTH_INDEFINITE
              ).show()
      }
  
      launch(Dispatchers.Main + handler) {
          userRepo.retrieveSelf()!!.let { name -&gt;
              userRepo.addAuthenticatedAccount(name)
              userRepo.setCurrentAccount(name)
          }
  
          activity?.apply {
              setResult(Activity.RESULT_OK, Intent())
              onBackPressed()
          }
      }
  }      
  </code></pre>
  
  <p>Big Thanks to Marko for helping me out!</p>