title: Coroutine Lifecycle Scope and isActive not working?
body: <p>I've been working with Coroutines thinking they work like Java Threads where the isAlive boolean is the same as checking for isInterrupted() in a long running operation such as writing a large file.</p>
  <p>In the past the Coroutine has worked as expected but in the current version 1.6.1-native-mt it doesn't seem to work like that any more. In the code below, the code inside the isAlive check doesn't seem to run even when I switch to the 1.6.1 non-multithreaded library.</p>
  <p>If this is the way it's supposed to work? What should the correct way of performing a clean-up after a job is cancelled?</p>
  <p>Also when attaching a Coroutine to a scope, in this case the Activity, shouldn't it be auto-cancelled when a new Activity is started? It seems to keep running if it isn't cancelled manually like in Java during the onPause()/onStop() phase.</p>
  <p>Kotlin Coroutine:</p>
  <pre class="lang-kotlin prettyprint-override"><code>import android.os.Bundle
  import android.util.Log
  import android.widget.Button
  import androidx.appcompat.app.AppCompatActivity
  import androidx.lifecycle.lifecycleScope
  import kotlinx.coroutines.*
  
  class TestActivity : AppCompatActivity() {
  
      private val tag = &quot;TEST&quot;
      lateinit var job: Job
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_test)
  
          val button = findViewById&lt;Button&gt;(R.id.stopbutton)
  
          job = lifecycleScope.launch(Dispatchers.IO) {
  
              (1..120).forEach {
  
                  // big long operation e.g. writing a large data file
                  Log.d(tag, &quot;ticking $it&quot;)
  
                  if ( !isActive ) {
                      // this code never happens after....
                      // 1. button is clicked to cancel job
                      // 2. app is killed
                      Log.d(tag, &quot;ticking stopped by button&quot;)
                      // clean up the partially written file
                      return@launch
                  }
  
                  delay(1000)
              }
  
          }
  
          button.setOnClickListener {
              job.cancel()
          }
  
      }
  
  }
  </code></pre>
  <p>Expected Output (Should be same as Java Thread code):</p>
  <pre><code>ticking 1
  ticking 2
  ticking 3
  ticking stopped
  </code></pre>
  <p>Actual Coroutine Output:</p>
  <pre><code>ticking 1
  ticking 2
  ticking 3
  </code></pre>
  <p>And the Java code for handling thread interrupts.</p>
  <pre class="lang-java prettyprint-override"><code>import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.os.SystemClock;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  
      protected Thread t = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          t = new Thread(){
  
              @Override
              public void run() {
  
                  for ( int x = 0; x &lt; 120; x++ ) {
                      Log.d(&quot;TEST&quot;, &quot;ticking &quot; + x);
  
                      if ( interrupted() ) {
                          Log.d(&quot;TEST&quot;, &quot;ticking stopped by button&quot;);
                          return;
                      }
  
                      // just for demoing
                      SystemClock.sleep(1000);
  
                  }
  
              }
          };
          t.start();
  
          Button btn = findViewById(R.id.button);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  t.interrupt();
              }
          });
      }
  }
  </code></pre>
  <p><strong>Update 1</strong></p>
  <p>After following @Sergio and @Pawel's quick replies I have removed delay() and replaced it with SystemClock.sleep() just like in Java to simulate long operations. Now it works as expected although I wonder why so many tutorials use it without highlighting the issue.</p>
  <p>Still trying to figure out what is the point of attaching a coroutine to a lifecycle scope if it doesn't auto cancel() onstop() such as described here:</p>
  <p><a href="https://medium.com/androiddevelopers/cancellation-in-coroutines-aa6b90163629" rel="nofollow noreferrer">https://medium.com/androiddevelopers/cancellation-in-coroutines-aa6b90163629</a></p>
  <p>Or maybe I'm missing the point?</p>