<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amanullah Sarker on Medium]]></title>
        <description><![CDATA[Stories by Amanullah Sarker on Medium]]></description>
        <link>https://medium.com/@amanullahsarker.engineer?source=rss-87f07a91f202------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LRekzO8v3KkDg0ra</url>
            <title>Stories by Amanullah Sarker on Medium</title>
            <link>https://medium.com/@amanullahsarker.engineer?source=rss-87f07a91f202------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 02:03:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amanullahsarker.engineer/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Preserving Composables Beyond the Viewport (A Practical Guide to PinnableContainer in Jetpack…]]></title>
            <link>https://medium.com/@amanullahsarker.engineer/preserving-composables-beyond-the-viewport-a-practical-guide-to-pinnablecontainer-in-jetpack-7e9be73a2e11?source=rss-87f07a91f202------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e9be73a2e11</guid>
            <category><![CDATA[prevent-recompose]]></category>
            <category><![CDATA[pin-content]]></category>
            <category><![CDATA[preserve-composable]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[pinnable-composable]]></category>
            <dc:creator><![CDATA[Amanullah Sarker]]></dc:creator>
            <pubDate>Tue, 21 Jul 2026 15:47:01 GMT</pubDate>
            <atom:updated>2026-07-21T15:55:06.047Z</atom:updated>
            <content:encoded><![CDATA[<h3>Preserving Composables Beyond the Viewport (A Practical Guide to PinnableContainer in Jetpack Compose)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3nIvZAe3PUvtadVRTGjLRw.png" /></figure><p>Jetpack Compose is incredibly efficient.</p><p>One of the biggest reasons is its ability to automatically compose and dispose UI as it enters and leaves the viewport. In LazyColumn, LazyRow, and other lazy layouts, off-screen items are disposed to reduce memory usage and keep scrolling smooth.</p><p>Most of the time, that’s exactly what we want.</p><p>But there are situations where disposing a composable every time it leaves the screen introduces unnecessary work, repeated network calls, and even a degraded user experience.</p><p>That’s where <strong>PinnableContainer</strong> comes in—a lesser-known API that gives you control over when a composable should remain alive.</p><h3>The Problem</h3><p>Consider a feed built with LazyColumn.</p><p>As users scroll:</p><ul><li>New items are composed.</li><li>Off-screen items are disposed.</li><li>When users scroll back, those items are composed again.</li></ul><p>This lifecycle is efficient.</p><p>However, not every composable is cheap to recreate.</p><p>Some composables perform expensive initialization or trigger side effects when they first enter the composition.</p><p>Examples include:</p><ul><li>Loading native advertisements</li><li>Making API requests</li><li>Initializing video players</li><li>Creating WebViews</li><li>Rendering PDF pages</li><li>Loading Google Maps</li><li>Starting animations</li><li>Restoring complex UI state</li></ul><p>Every disposal means doing that work again.</p><p>Sometimes, that’s unnecessary.</p><h3>Meet PinnableContainer</h3><p>Compose exposes a CompositionLocal called:</p><pre>LocalPinnableContainer</pre><p>When your composable is inside a lazy layout, it provides access to the current item’s PinnableContainer.</p><pre>val pinnableContainer = LocalPinnableContainer.current</pre><p>You can then pin the current item.</p><pre>val handle = pinnableContainer?.pin()</pre><p>Pinning tells Compose:</p><blockquote><strong><em>“Even if this item leaves the viewport, keep its composition alive until I release it.”</em></strong></blockquote><p>Later, when the item no longer needs to stay alive:</p><pre>handle.release()</pre><p>Compose is then free to dispose it normally.</p><h3>A Small Reusable Wrapper</h3><p>Instead of calling pin() everywhere, I created a reusable composable.</p><pre>@Composable<br>fun PreservedComposable(<br>    content: @Composable () -&gt; Unit<br>) {<br>    val pinnableContainer = LocalPinnableContainer.current</pre><pre>    val pinnedHandle = remember(pinnableContainer) {<br>        pinnableContainer?.pin()<br>    }</pre><pre>    DisposableEffect(pinnedHandle) {<br>        onDispose {<br>            pinnedHandle?.release()<br>        }<br>    }</pre><pre>    content()<br>}</pre><p>Now preserving any composable is as simple as:</p><pre>PreservedComposable {<br>    MyComposable()<br>}</pre><p>The wrapper automatically:</p><ul><li>pins once</li><li>survives recompositions</li><li>releases correctly</li><li>becomes a no-op outside a PinnableContainer</li></ul><h3>Why remember?</h3><p>Without remember, every recomposition would execute:</p><pre>pin()</pre><p>again.</p><p>That would create multiple active pins for the same composable.</p><p>Using:</p><pre>remember(pinnableContainer)</pre><p>ensures the pin is created only once for the current container.</p><h3>Why DisposableEffect?</h3><p>pin() allocates a resource.</p><p>Compose expects you to release it.</p><pre>DisposableEffect(pinnedHandle) {<br>    onDispose {<br>        pinnedHandle?.release()<br>    }<br>}</pre><p>This guarantees proper cleanup and prevents pinned items from staying alive longer than intended.</p><h3>“Can I Use It Anywhere?”</h3><p>Yes — with an important detail.</p><p>Your wrapper can be used around <strong>any composable</strong>.</p><p>If the current hierarchy provides a PinnableContainer (such as inside a LazyColumn), the composable will be pinned.</p><p>If no PinnableContainer exists, LocalPinnableContainer.current is simply null, and the wrapper behaves like a normal composable with virtually no overhead.</p><p>That means you don’t need different implementations for lazy and non-lazy content.</p><h3>Real-World Use Cases</h3><p>This API becomes valuable when recreating a composable is more expensive than temporarily keeping it alive.</p><h3>1. Native Ads (One of the Best Use Cases)</h3><p>Imagine your feed displays a native ad every 10 items.</p><p>When the ad enters the screen:</p><pre>Composable Created<br>        ↓<br>Request Native Ad<br>        ↓<br>Ad Loaded</pre><p>Now the user scrolls away.</p><p>Compose disposes the item.</p><p>When the user scrolls back:</p><pre>Composable Created Again<br>        ↓<br>Another Ad Request</pre><p>The ad is requested repeatedly simply because the item left the viewport.</p><p>By pinning the composable, the already-loaded ad remains alive, avoiding unnecessary reloads and improving the scrolling experience.</p><h3>2. Prevent Duplicate API Calls</h3><p>Suppose each list item loads additional details only when it first appears.</p><p>Without preservation:</p><pre>Visible<br>↓</pre><pre>API Request</pre><pre>↓</pre><pre>Scroll Away</pre><pre>↓</pre><pre>Disposed</pre><pre>↓</pre><pre>Scroll Back</pre><pre>↓</pre><pre>API Request Again</pre><p>If the result isn’t cached elsewhere, users may trigger the same request repeatedly while scrolling.</p><p>Keeping the composition alive avoids unnecessary requests and reduces backend traffic.</p><h3>3. Video Players</h3><p>Initializing ExoPlayer involves:</p><ul><li>creating the player</li><li>attaching surfaces</li><li>buffering content</li><li>restoring playback state</li></ul><p>Recreating all of that every time a user scrolls back isn’t ideal.</p><p>Pinning keeps the player alive while needed.</p><h3>4. PDF Viewers</h3><p>Rendering PDF pages is expensive.</p><p>Without preservation:</p><ul><li>renderer recreated</li><li>bitmap regenerated</li><li>page rendered again</li></ul><p>Keeping the composable alive avoids repeating heavy rendering work.</p><h3>5. WebView</h3><p>A recreated WebView usually means:</p><ul><li>page reload</li><li>JavaScript context restart</li><li>lost scroll position</li><li>reinjected bridges</li></ul><p>Pinning helps preserve that expensive initialization.</p><h3>6. Google Maps</h3><p>Maps require time to initialize.</p><p>Keeping the composition alive preserves:</p><ul><li>camera position</li><li>zoom level</li><li>loaded tiles</li><li>markers</li></ul><p>without another initialization cycle.</p><h3>7. Camera Preview</h3><p>Camera setup isn’t instant.</p><p>Pinning avoids repeatedly:</p><ul><li>opening the camera</li><li>creating preview sessions</li><li>recreating surfaces</li></ul><h3>8. Long-running Animations</h3><p>Animations often restart after recomposition.</p><p>Keeping the composable alive allows animations to continue smoothly instead of starting from the beginning.</p><h3>9. Large Editable Forms</h3><p>Imagine users filling a long form inside a LazyColumn.</p><p>While scrolling, they may temporarily move the active field off-screen.</p><p>Pinning helps preserve:</p><ul><li>focus</li><li>cursor position</li><li>text selection</li><li>editing state</li></ul><p>resulting in a much smoother experience.</p><h3>10. Heavy Custom Components</h3><p>Components such as:</p><ul><li>financial charts</li><li>image editors</li><li>drawing canvases</li><li>signature pads</li><li>custom graphs</li></ul><p>can require expensive initialization or calculations.</p><p>Instead of rebuilding everything during rapid scrolling, pinning lets them remain composed until they’re no longer needed.</p><h3>Should You Use It Everywhere?</h3><p>Definitely not.</p><p>PinnableContainer isn&#39;t intended to replace Compose&#39;s lazy recycling.</p><p>Compose disposes off-screen content for a reason: <strong>performance and memory efficiency.</strong></p><p>Every pinned composable stays in memory until its pin is released.</p><p>Use it only when the cost of recreating the composable is greater than the cost of keeping it alive.</p><p>A good rule of thumb is:</p><blockquote><strong><em>Pin behavior, not UI.</em></strong></blockquote><blockquote><em>Preserve only those composables whose recreation causes noticeable performance issues, duplicate work, or a poor user experience.</em></blockquote><h3>Key Takeaways</h3><p>✅ LazyColumn automatically disposes items outside the viewport.</p><p>✅ PinnableContainer allows an item to remain composed after leaving the screen.</p><p>✅ A reusable PreservedComposable makes the API simple to adopt.</p><p>✅ The wrapper safely becomes a no-op outside lazy containers.</p><p>✅ Great use cases include:</p><ul><li>Native Ads</li><li>Preventing duplicate API calls</li><li>Video Players</li><li>PDF Viewers</li><li>WebViews</li><li>Google Maps</li><li>Camera Preview</li><li>Long-running Animations</li><li>Large Forms</li><li>Heavy Custom Components</li></ul><h3>Final Thoughts</h3><p>One thing I really appreciate about Jetpack Compose is that it gives developers low-level APIs for advanced scenarios without forcing everyone to use them.</p><p>PinnableContainer is one of those APIs.</p><p>Most applications will never need it. But when you’re dealing with expensive composables, repeated initialization, duplicate network requests, or complex interactive components, it can make a noticeable difference in both performance and user experience.</p><p>It’s also a great reminder that <strong>preserving state and preserving composition are two different concepts</strong>. APIs like remember and rememberSaveable help you retain state, while PinnableContainer lets you retain the composable itself for as long as it truly needs to stay alive.</p><p>I’d love to hear how you’ve handled similar challenges in Compose. Have you used PinnableContainer, or do you rely on other techniques to avoid unnecessary recomposition and recreation? Let&#39;s discuss.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e9be73a2e11" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>