title: How to listen for lifecycle in Jetpack Compose
body: <p>I am creating an app using the Jetpack Compose. The app does some networking search in the background. I wanted to cancel the search if the user decided to press the back button. so I did it using <code>DisposableEffect</code>.</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Composable
  fun SecondScreen() {
      val vm: MainViewModel = hiltViewModel()
  
      DisposableEffect(key1 = vm) {
          onDispose {
              vm.cancelSearch()
          }
      }
  }
  </code></pre>
  <p>I know that <code>onDispose</code> will be called when the composable destroyed. But now the search getting canceled everytime when I rotate the screen. I can understand that because the composable destroyed and re-created.</p>
  <p>I wonder if I could some how detect the screen rotation lifecyle, maybe I can prevent this behaviour. Or is there a better way to cancel the search when user press back?</p>