title: Android screen rotation during API call gives kotlinx.coroutines.JobCancellationException
body: <p>I get this error -</p>
  <pre><code>System.err: kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled; job=StandaloneCoroutine{Cancelling}
  </code></pre>
  <p>Here is my fragment code -</p>
  <pre><code>@AndroidEntryPoint
  class LoginView : Fragment(R.layout.login_view) {
  
      private val viewModel by viewModels&lt;LoginViewModel&gt;()
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          view.login_button.setOnClickListener {
              val email = view.email_input_layout.text.toString()
              val password = view.password_input_layout.text.toString()
          
              viewModel.loginUser(email, password).observe(viewLifecycleOwner, {
                  // ….
              })
          }
      }
  }
  </code></pre>
  <p>Here is my ViewModel code -</p>
  <pre><code>fun loginUser(email: String, password: String)= repo.loginUser(Action.LoginUser(email, password)).map {
      when (it) {
         // …                    
       }
   }.asLiveData(Dispatchers.Default + viewModelScope.coroutineContext)
  </code></pre>
  <p>Here is the repo code -</p>
  <pre><code>fun loginUser(action: Action) = flow {
    when (action) {
       is Action.LoginUser -&gt; {
              emit(Result.Loading)
              val result = remoteSource.loginUser(LoginModel(action.email, action.password))
              emit(result)
       }
    }
  }
  </code></pre>
  <p>Here is the Remote Data Source Code -</p>
  <pre><code>override suspend fun loginUser(loginModel: LoginModel): Result {
      try {
           Log.e(TAG, “Going to do api call&quot; + Thread.currentThread().name)
           val result = withContext(Dispatchers.IO) {
                    Log.e(TAG, “Doing api call&quot; + Thread.currentThread().name)
                    apiInterface.loginUser(loginModel)
           }
           if (result.isSuccessful) {
              Log.e(TAG, “Api call success&quot;)
              return Result.Success(result.body())
           } else {
              Log.e(TAG, “Api call error”)
              return Result.Error(result.code())
           }
       } catch (e: Exception) {
         // Here I catch the JobCancellationException
          e.printStackTrace()
       }
       return Result.Error(UNKNOWN_ERROR)
  }
  </code></pre>
  <p>And finally here is the Api Interface -</p>
  <pre><code> @POST(&quot;login&quot;)
   suspend fun loginUser(@Body loginModel: LoginModel): Response&lt;LoginResponseModel&gt;
  </code></pre>
  <p>I have mocked the API response to be delivered in 20 seconds.</p>
  <p>Here is the problem -</p>
  <p>I hit the login button and the login call is happening. Now I see the logs -</p>
  <pre><code>Going to do api call main
  
  Doing api call DefaultDispatcher-worker-1
  </code></pre>
  <p>As you see I am on the <code>main</code> thread until a call is requested and thread switching happens when I make the call.</p>
  <p>Now I rotate the phone in the middle of the API call and my view is created new which is expected and I don't see any log and within few seconds I get the <code>JobCancellationException</code> which I catch in the <code>RemoteDataSource</code>. Could anyone guide me what exactly is the mistake here</p>