title: How to use Coroutine inside a FloatingActionButton/Button Click event
body: <p>I have a FloatingActionButton, which when clicked will download few URL's from the web. I want to start this function from inside the setOnClickListener, and then let it does all the processing in the background.</p>
  <p>I already did what I wanted to do using AsyncTask, but then someone suggested to use coroutines.</p>
  <p>Inside the activity:</p>
  <pre><code>findViewById&lt;FloatingActionButton&gt;(R.id.fab).setOnClickListener { view -&gt;
             
              val currentChapUrl = &quot;https://www.novel.com/chap1.html&quot;  
              GlobalScope.launch { downloadChapters(applicationContext,currentChapUrl) } 
          }
  </code></pre>
  <p>The downloadChapters function looks like this:</p>
  <pre><code>suspend fun downloadChapters(context : Context, startingChapUrl : String) : String {
     //download chapters.
     //show some toast messages
  
  }
  </code></pre>
  <p>The app crashes on clicking the button.</p>
  <p>Logcat:</p>
  <pre><code>2020-08-01 17:03:38.559 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:38.559 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.095 6937-6937/com.example.daoreader I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
  2020-08-01 17:03:39.124 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.125 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.163 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.163 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.208 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.209 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
      
      
      --------- beginning of crash
  2020-08-01 17:03:39.211 6937-7086/com.example.daoreader E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
      Process: com.example.daoreader, PID: 6937
      java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
          at android.os.Handler.&lt;init&gt;(Handler.java:200)
          at android.os.Handler.&lt;init&gt;(Handler.java:114)
          at android.widget.Toast$TN$2.&lt;init&gt;(Toast.java:336)
          at android.widget.Toast$TN.&lt;init&gt;(Toast.java:336)
          at android.widget.Toast.&lt;init&gt;(Toast.java:103)
          at android.widget.Toast.makeText(Toast.java:256)
          at com.example.daoreader.WebviewActivity.downloadChapters(WebviewActivity.kt:65)
          at com.example.daoreader.WebviewActivity$onCreate$1$1.invokeSuspend(WebviewActivity.kt:32)
          at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
          at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
          at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
          at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
          at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
      
      
      --------- beginning of system
  2020-08-01 17:03:39.257 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.257 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.307 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.307 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.394 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.394 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.487 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.487 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.565 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.566 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.581 6937-6985/com.example.daoreader D/EGL_emulation: eglMakeCurrent: 0xaa205000: ver 2 0 (tinfo 0xaa2034c0)
  2020-08-01 17:03:39.595 6937-6985/com.example.daoreader D/EGL_emulation: eglMakeCurrent: 0xaa205000: ver 2 0 (tinfo 0xaa2034c0)
  2020-08-01 17:03:39.658 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.658 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:39.714 6937-6985/com.example.daoreader D/EGL_emulation: eglMakeCurrent: 0xaa205000: ver 2 0 (tinfo 0xaa2034c0)
  2020-08-01 17:03:39.727 6937-6985/com.example.daoreader D/OpenGLRenderer: endAllActiveAnimators on 0x9232d500 (RippleDrawable) with handle 0x92021280
  2020-08-01 17:03:40.051 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:40.051 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:40.094 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  2020-08-01 17:03:40.094 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
  </code></pre>