<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: atif rehman</title>
    <description>The latest articles on DEV Community by atif rehman (@atif_rehman_dec1a8f2103de).</description>
    <link>https://dev.to/atif_rehman_dec1a8f2103de</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3404864%2F8f93a210-ef1b-4b47-8c6a-e891ecec240d.png</url>
      <title>DEV Community: atif rehman</title>
      <link>https://dev.to/atif_rehman_dec1a8f2103de</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atif_rehman_dec1a8f2103de"/>
    <language>en</language>
    <item>
      <title>Mastering Launched Effect in Jetpack Compose: What, Why &amp; How</title>
      <dc:creator>atif rehman</dc:creator>
      <pubDate>Fri, 01 Aug 2025 06:23:07 +0000</pubDate>
      <link>https://dev.to/atif_rehman_dec1a8f2103de/mastering-launched-effect-in-jetpack-compose-what-why-how-3kh1</link>
      <guid>https://dev.to/atif_rehman_dec1a8f2103de/mastering-launched-effect-in-jetpack-compose-what-why-how-3kh1</guid>
      <description>&lt;p&gt;Jetpack Compose has transformed the way we build UI in Android — but if you're not using &lt;code&gt;LaunchedEffect&lt;/code&gt; properly, you're probably writing buggy or inefficient code. &lt;/p&gt;

&lt;p&gt;Let’s break it down: &lt;strong&gt;What is &lt;code&gt;LaunchedEffect&lt;/code&gt;, why is it important, and how do you use it correctly?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is &lt;code&gt;LaunchedEffect&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LaunchedEffect&lt;/code&gt; is a &lt;strong&gt;side-effect handler&lt;/strong&gt; in Jetpack Compose. It lets you &lt;strong&gt;launch coroutines tied to the lifecycle of the composable&lt;/strong&gt;, safely.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It only runs when its &lt;strong&gt;key(s)&lt;/strong&gt; change, or when it first enters the composition.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;kotlin&lt;br&gt;
LaunchedEffect(Unit) {&lt;br&gt;
    // This runs once when the composable enters composition&lt;br&gt;
    delay(1000)&lt;br&gt;
    println("Effect triggered")&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use LaunchedEffect?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You use LaunchedEffect for things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching data when a screen loads&lt;/li&gt;
&lt;li&gt;Triggering animations&lt;/li&gt;
&lt;li&gt;Listening to a Flow&lt;/li&gt;
&lt;li&gt;Running one-time setup logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;It’s lifecycle-aware&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Automatically cancels if the composable leaves the UI&lt;/li&gt;
&lt;li&gt; Safer than launching coroutines in init or onCreate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t use LaunchedEffect for long-lived state&lt;br&gt;
It's designed for one-off jobs like setup or reaction, not holding ongoing state.&lt;/p&gt;

&lt;p&gt;Use remember or rememberCoroutineScope() if you need a long-lived coroutine scope within Compose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keys Matter!&lt;/strong&gt;&lt;br&gt;
The key parameter determines when the effect should re-run.&lt;/p&gt;

&lt;p&gt;LaunchedEffect(Unit) { ... }         // Runs once&lt;br&gt;
LaunchedEffect(userId) { ... }       // Runs when userId changes&lt;br&gt;
LaunchedEffect(key1, key2) { ... }   // Runs when either changes&lt;br&gt;
Don’t use Unit if you want the effect to re-trigger — pass a meaningful key!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use LaunchedEffect for one-shot tasks: data fetching, collecting flows, starting animations.&lt;/li&gt;
&lt;li&gt;Always give it a key if you want it to react to changes.&lt;/li&gt;
&lt;li&gt;Avoid logic inside Composables that causes uncontrolled recomposition.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
