<?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 Fernandes K J on Medium]]></title>
        <description><![CDATA[Stories by Fernandes K J on Medium]]></description>
        <link>https://medium.com/@fernooxplozd?source=rss-b5eeb3a2c5b5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2BA0NH7xKGVjP1FT</url>
            <title>Stories by Fernandes K J on Medium</title>
            <link>https://medium.com/@fernooxplozd?source=rss-b5eeb3a2c5b5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 23:47:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fernooxplozd/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[“Did the SDK Finish?” How We Fixed Our Android SDK’s Response Callback]]></title>
            <link>https://medium.com/@fernooxplozd/did-the-sdk-finish-how-we-fixed-our-android-sdks-response-callback-0de754ab77ac?source=rss-b5eeb3a2c5b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/0de754ab77ac</guid>
            <category><![CDATA[sdk]]></category>
            <category><![CDATA[android-sdk]]></category>
            <category><![CDATA[callback-function]]></category>
            <category><![CDATA[android-jetpack-compose]]></category>
            <category><![CDATA[interfaces]]></category>
            <dc:creator><![CDATA[Fernandes K J]]></dc:creator>
            <pubDate>Fri, 06 Nov 2026 17:49:49 GMT</pubDate>
            <atom:updated>2026-11-06T17:49:49.975Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A story about one SDK, one frustrated app team, and two clean ways to send results back to the app.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HyWFUOncnbJxSbF8Q_Exgg.png" /></figure><h3>The story</h3><p>We built a small SDK called <strong>SnapScan</strong>. An app calls it, SnapScan opens a camera screen, and the user scans a document.</p><p>Our first user was an expense app called <strong>Spendly</strong>. Soon their team asked us:</p><blockquote><em>“When the scan finishes… how does our app find out? </em>We had no good answer. Sometimes they got the result, sometimes they got nothing (for example, when the user pressed back), and sometimes they got it twice.</blockquote><h3>Finding the solution</h3><p>We wrote down what the app needs to know:</p><ul><li>✅ The scan <strong>succeeded</strong> (and what was scanned)</li><li>🚫 The user <strong>cancelled</strong></li><li>❌ Something <strong>failed</strong></li></ul><p>We also agreed on three rules:</p><ol><li>Call back <strong>once</strong>, never twice.</li><li>Call back <strong>every time</strong>, even when the user presses back.</li><li><strong>Forget</strong> the callback afterwards, so the app’s objects aren’t kept in memory.</li></ol><p>The simplest tool that fits is a <strong>callback interface</strong>: the SDK defines it, and the app implements it.</p><h4>The implementation</h4><p>1. The SDK defines the callback:</p><pre>interface ScanCallback {<br>    fun onSuccess(documentId: String)<br>    fun onCancelled()<br>    fun onError(message: String)<br>}</pre><p>2. The SDK stores it and calls it once:</p><pre>object SnapScan {<br>    private var callback: ScanCallback? = null<br><br>    fun startScan(activity: Activity, callback: ScanCallback) {<br>        this.callback = callback<br>        activity.startActivity(Intent(activity, ScanActivity::class.java))<br>    }<br><br>    internal fun sendResult(action: ScanCallback.() -&gt; Unit) {<br>        val cb = callback ?: return<br>        callback = null               <br>        cb.action()<br>    }<br>}</pre><p>3. The SDK screen sends the result:</p><pre>class ScanActivity : ComponentActivity() {<br><br>    fun onScanDone(documentId: String) {<br>        SnapScan.sendResult { onSuccess(documentId) }<br>        finish()<br>    }<br><br>    override fun onDestroy() {<br>        super.onDestroy()<br>        if (isFinishing) SnapScan.sendResult { onCancelled() }<br>    }<br>}</pre><p>4. The consumer app uses it:</p><pre>SnapScan.startScan(this, object : ScanCallback {<br>    override fun onSuccess(documentId: String) { showReceipt(documentId) }<br>    override fun onCancelled() { showMessage(&quot;Scan cancelled&quot;) }<br>    override fun onError(message: String) { showMessage(message) }<br>})</pre><p>That’s it. Spendly now always gets exactly one answer.</p><h3>Another way: a function callback from the SDK’s ViewModel</h3><p>If your SDK screens use a <strong>ViewModel</strong>, the ViewModel already knows when the work is done, so it can send the result itself.</p><p>Instead of an interface with three methods, the app passes <strong>one function</strong> that receives <strong>one result</strong>.</p><h4>1. One result type</h4><pre>sealed class ScanResult {<br>    data class Success(val documentId: String) : ScanResult()<br>    object Cancelled : ScanResult()<br>    data class Error(val message: String) : ScanResult()<br>}</pre><p>2. The SDK accepts a function:</p><pre>object SnapScan {<br>    internal var onResult: ((ScanResult) -&gt; Unit)? = null<br><br>    fun startScan(activity: Activity, onResult: (ScanResult) -&gt; Unit) {<br>        this.onResult = onResult<br>        activity.startActivity(Intent(activity, ScanActivity::class.java))<br>    }<br>}</pre><p>3. The SDK’s ViewModel calls it:</p><pre>class ScanViewModel : ViewModel() {<br><br>    fun onScanDone(documentId: String) = send(ScanResult.Success(documentId))<br><br>    fun onScanFailed(message: String) = send(ScanResult.Error(message))<br><br>    private fun send(result: ScanResult) {<br>        val callback = SnapScan.onResult ?: return<br>        SnapScan.onResult = null<br>        callback(result)<br>    }<br><br>    override fun onCleared() {<br>        send(ScanResult.Cancelled)<br>    }<br>}</pre><p>onCleared() runs when the screen is truly closed, but not on rotation. That makes it the safety net for back presses.</p><h4>4. The app uses it:</h4><pre>SnapScan.startScan(this) { result -&gt;<br>    when (result) {<br>        is ScanResult.Success -&gt; showReceipt(result.documentId)<br>        is ScanResult.Cancelled -&gt; showMessage(&quot;Scan cancelled&quot;)<br>        is ScanResult.Error -&gt; showMessage(result.message)<br>    }<br>}</pre><h3>Key takeaways</h3><ul><li>The SDK <strong>defines</strong> the callback, and the app <strong>implements</strong> it.</li><li>Call back <strong>once</strong>, call back <strong>always</strong>, then <strong>forget</strong> the callback.</li><li>Use an <strong>interface</strong> for a classic setup, or a <strong>function from the ViewModel</strong> for a modern Kotlin setup.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0de754ab77ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>