<?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 AravindMani HariKrishnan on Medium]]></title>
        <description><![CDATA[Stories by AravindMani HariKrishnan on Medium]]></description>
        <link>https://medium.com/@maniaravind76?source=rss-43d969f93daa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*o4svRxZ0IoIVTxBLmasvCQ.jpeg</url>
            <title>Stories by AravindMani HariKrishnan on Medium</title>
            <link>https://medium.com/@maniaravind76?source=rss-43d969f93daa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:01:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maniaravind76/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[Atomic Types in Android: A Practical Guide to High-Performance Concurrency]]></title>
            <link>https://medium.com/@maniaravind76/atomic-types-in-android-a-practical-guide-to-high-performance-concurrency-c879ceb83ed3?source=rss-43d969f93daa------2</link>
            <guid isPermaLink="false">https://medium.com/p/c879ceb83ed3</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[atomic]]></category>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin-beginners]]></category>
            <dc:creator><![CDATA[AravindMani HariKrishnan]]></dc:creator>
            <pubDate>Sat, 19 Jul 2025 13:37:11 GMT</pubDate>
            <atom:updated>2025-07-19T13:37:11.749Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MNwTNm3VY8qxFq-k2gZ0IA.jpeg" /></figure><p>🔬Atomic Types in Android: A Practical Guide to High-Performance Concurrency</p><p>Unlock the full potential of Android’s low-level concurrency with Atomic classes like AtomicBoolean, AtomicInteger, and more. Discover how they work, when to use them, and explore modern alternatives for writing safer, faster multithreaded code.</p><h3>📘 <strong>What You’ll Learn</strong></h3><ol><li>What Are Atomic Types?</li><li>Why Use Atomic Types?</li><li>Core Atomic Classes in Android</li><li>Common Use Cases in Android Apps</li><li>Deep Dive: compareAndSet vs getAndSet</li><li>Atomic Types vs Volatile</li><li>Atomic Types vs StateFlow / Mutex</li><li>Thread-Safe Patterns using Atomic Types</li><li>Best Practices &amp; Pitfalls</li><li>Summary &amp; Alternatives</li></ol><h3>1. 🧠 What Are Atomic Types?</h3><p>Atomic types are <strong>thread-safe wrappers</strong> for primitive data types and references that allow <strong>atomic operations</strong> — operations that are <strong>completed in a single step without interruption</strong>.</p><p>They belong to java.util.concurrent.atomic and include:</p><ul><li>AtomicBoolean</li><li>AtomicInteger</li><li>AtomicLong</li><li>AtomicReference&lt;T&gt;</li></ul><h3>. ⚠️ Why Use Atomic Types?</h3><p>Concurrency bugs are <strong>hard to detect and reproduce</strong>. Using Atomic* classes provides:</p><ul><li>Lock-free synchronization (no synchronized keyword needed)</li><li>Lower overhead than traditional locks</li><li>Fast, thread-safe updates for simple variables</li></ul><p>These are ideal for <strong>flags</strong>, <strong>counters</strong>, or <strong>state transitions</strong> across threads.</p><h3>3. 🔧 Core Atomic Classes in Android</h3><h4>🔹 AtomicBoolean</h4><p>Useful for toggling flags, debounce handling, preventing re-entry:</p><pre>val isExecuting = AtomicBoolean(false)<br><br>fun runTaskOnce() {<br>    if (isExecuting.compareAndSet(false, true)) {<br>        // safe to proceed<br>        performWork()<br>        isExecuting.set(false)<br>    }<br>}</pre><h4>🔹 AtomicInteger / AtomicLong</h4><p>Useful for thread-safe counters or indexes:</p><pre>val index = AtomicInteger(0)<br>val id = index.getAndIncrement()</pre><h4>🔹 AtomicReference&lt;T&gt;</h4><p>For shared object references:</p><pre>val currentUser = AtomicReference&lt;User&gt;()<br>fun updateUser(user: User) {<br>    currentUser.set(user)<br>}</pre><h4>🔹 AtomicStampedReference / AtomicMarkableReference (Advanced)</h4><p>These provide <strong>versioning and ABA protection</strong> for high-performance algorithms, mostly used in <strong>custom concurrent data structures</strong>.</p><h3>4. 🚀 Common Use Cases in Android</h3><ul><li>Preventing double-clicks in UI</li><li>One-time execution of background task</li><li>Custom debouncing mechanisms</li><li>Service lifecycle flags (e.g., isServiceRunning)</li><li>Worker thread coordination</li><li>Retry flags</li></ul><h3>5. 🧪 compareAndSet 🆚getAndSet</h3><pre>|     <strong>Method      </strong>|              <strong>Behavior</strong><br>| --------------- | -----------------------------------------<br>| `<em>compareAndSet</em>` | Changes value only if it matches expected <br>|                 |  (atomic check+set)<br><br>| `<em>getAndSet</em>`     | Always sets the new value and returns the <br>|                 | old one (no condition)</pre><p><strong>Example:</strong></p><pre>val lock = AtomicBoolean(false)<br><br>if (lock.compareAndSet(false, true)) {<br>    // Lock acquired<br>    doSomething()<br>    lock.set(false)<br>}</pre><h3>6. 🆚 Atomic Types vs volatile</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*SyrJUz-A7TdnZLS2lsNGQw.png" /></figure><p>Use volatile if you only need visibility across threads, and <strong>no compound actions</strong> (like incrementing).</p><h3>7. 🔄 Atomic Types vs StateFlow / Mutex</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*laLzRuJuxJQO9jGO9_YxjQ.png" /></figure><p>🟩 Use Atomic* for background logic<br> 🟦 Use StateFlow for UI-driven state<br> 🟥 Use Mutex when critical sections must be locked</p><h3>8. 🧵 Thread-Safe Pattern: One-Time Initialization</h3><pre>val initialized = AtomicBoolean(false)<br><br>fun initialize() {<br>    if (initialized.compareAndSet(false, true)) {<br>        // Do init work once<br>    }<br>}</pre><p>Another pattern — safe lazy loading:</p><pre>val instance = AtomicReference&lt;MyService?&gt;()<br><br>fun getInstance(): MyService {<br>    return instance.updateAndGet {<br>        it ?: MyService()<br>    }!!<br>}</pre><h3>9. ✅ Best Practices &amp; Pitfalls</h3><h4>✔ Best Practices:</h4><ul><li>Use compareAndSet for safe state transitions</li><li>Prefer StateFlow or LiveData for UI state</li><li>Use AtomicReference for shared mutable objects</li></ul><h4>❌ Avoid:</h4><ul><li>Using Atomic* with large complex objects</li><li>Overusing them where structured concurrency is better</li><li>Mixing atomic types with synchronized/locks (leads to confusion)</li></ul><h3>10.🔚 Summary</h3><pre>| Use This...       |         When You Need...               |<br>| ----------------- | -------------------------------------- |<br>| `AtomicBoolean`   | Thread-safe flag                       |<br>| `AtomicInteger`   | Counter across threads                 |<br>| `AtomicReference` | Shared reference to object             |<br>| `StateFlow`       | UI-observable shared state (ViewModel) |<br>| `Mutex`           | Exclusive access to a block of code    |</pre><h3>🧠 Final Thoughts</h3><p>Atomic types are simple yet powerful tools for handling concurrency in Android. They help you write safer, lock-free code when managing shared state across threads.</p><p>While newer tools like StateFlow and Mutex are great for modern apps, atomic types still have a place — especially in services, background tasks, and SDKs.</p><blockquote><em>Use them wisely to avoid race conditions and keep your code clean, fast, and thread-safe.</em></blockquote><p>📝<strong>Have you used atomic functions in your Android projects?</strong><br> Share your experience in the comments below……</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c879ceb83ed3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android AIDL for IPC: Seamless Communication Between Two apps]]></title>
            <link>https://medium.com/@maniaravind76/android-aidl-for-ipc-seamless-communication-between-two-apps-955a3feca4f1?source=rss-43d969f93daa------2</link>
            <guid isPermaLink="false">https://medium.com/p/955a3feca4f1</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[aidl]]></category>
            <category><![CDATA[interprocesscommunication]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[AravindMani HariKrishnan]]></dc:creator>
            <pubDate>Sun, 18 May 2025 15:54:32 GMT</pubDate>
            <atom:updated>2025-05-18T16:13:27.508Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7xxl6qJVI_ImrTaUwJNj3g.png" /></figure><p>In Android development, when you need two apps (or two different processes) to talk to each other, you need <strong>Inter-Process Communication (IPC)</strong>. Android provides a powerful tool for this — <strong>AIDL (Android Interface Definition Language)</strong>.</p><p>This guide will show you step-by-step how to implement AIDL for communication between <strong>two apps</strong>, AppA and AppB, using <strong>Kotlin</strong>.</p><h3>What is AIDL?</h3><p><strong>AIDL</strong> allows you to define an interface in .aidl files and use it to communicate across app or process boundaries. It supports:</p><ul><li>Primitive types: int, long, boolean, float, double</li><li>String types: String, CharSequence</li><li>Custom types implementing Parcelable</li></ul><p>This makes it ideal for high-performance IPC needs.</p><h3>🧩 Our Scenario</h3><p>We want <strong>AppA</strong> to send a string value to <strong>AppB</strong>, and <strong>AppB</strong> will store that string as the current emailId. We’ll define a method</p><pre>void getEmailId(String value);</pre><p>Let’s begin!</p><h3>📱 AppA — The Client App</h3><p>This app <strong>binds</strong> to the service in AppB and <strong>calls</strong> the getEmailId() method via AIDL.</p><h3>1️⃣ Create the AIDL Interface</h3><p><strong>Step 1:</strong> Create a new package named sendDataToService</p><pre>Right-click `java` → New → Package → name it `sendDataToService`</pre><p><strong>Step 2:</strong> Add an AIDL file named UserInfo.aidl:</p><pre>Right-click on `sendDataToService` → New → AIDL → AIDL File → Name: `UserInfo`</pre><p><strong>Step 3:</strong> Replace default content with:</p><pre>// UserInfo.aidl<br>package sendDataToService;<br><br>interface UserInfo{<br>    void getEmailId(String value);<br>}</pre><p><strong>Step 4:</strong> Clean the project after creating or editing the AIDL file to regenerate the required Java/Kotlin bindings.</p><h3>2️⃣ Bind to AIDL Service</h3><p>You need to define a <strong>ServiceConnection</strong> object to manage the connection lifecycle.</p><p>Here’s the <strong>full Kotlin class code</strong> for managing the connection and calling the method:</p><pre>package com.appa<br>import android.content.*<br>import android.os.*<br>import android.util.Log<br>import android.widget.Toast<br>import androidx.appcompat.app.AppCompatActivity<br>import sendDataToService.UserInfo<br><br>class MainActivity : AppCompatActivity() {<br><br>     private var userInfo: UserInfo? = null<br><br>   private val connection = object : ServiceConnection {<br><br>    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {   <br>         userInfo = UserInfo.Stub.asInterface(service)<br>       Log.d(&quot;AppA&quot;, &quot;Service Connected&quot;)<br>         try {<br>          // Example call to remote service<br>          userInfo?.getEmailId(&quot;arun420@gmail.com&quot;)<br>           Log.d(&quot;AppA&quot;, &quot;Sent email to AppB&quot;)<br>         } catch (e: RemoteException) {<br>                e.printStackTrace()<br>         }<br>     }<br><br>     override fun onServiceDisconnected(name: ComponentName?) {<br>            internalTerminalInfo = null<br>         Log.d(&quot;AppA&quot;, &quot;Service Disconnected&quot;)        <br>     }<br><br>  }<br><br>  private fun getIntent(): Intent {<br>    val intent = Intent(&quot;UserInfo_service&quot;)<br>    intent.setPackage(&quot;com.appb&quot;) // Replace with AppB&#39;s actual package name<br>    return intent<br>  }<br><br>  override fun onStart() {<br>   super.onStart()<br>   bindService(getIntent(), connection, Context.BIND_AUTO_CREATE)<br>  }<br><br>  override fun onStop() {<br>   super.onStop()<br>   unbindService(connection)<br>  }<br><br>}</pre><h3>📲 AppB —The Service App</h3><p>1️⃣ Create Matching AIDL Interface<br><strong>Very important:</strong> The package name and file name must be <strong>identical</strong> to AppA’s AIDL.</p><p><strong>Steps:</strong></p><ul><li>Create the package sendDataToService</li><li>Create UserInfo.aidl inside it with same content</li><li>Paste the same interface:</li><li>Clean the project</li></ul><pre>// UserInfo.aidl<br>package sendDataToService;<br><br>interface UserInfo{<br>    void getEmailId(String value);<br>}</pre><p>✅ Ensure <strong>package and file name match exactly</strong> in both apps.</p><p>2️⃣ Create the AIDL Service</p><pre>package com.appb.services<br>import android.app.Service<br>import android.content.Intent<br>import android.os.IBinder<br>import sendDataToService.UserInfo<br><br><br>class UserInfoService : Service() {<br><br>   private var currentEmail: String = &quot;&quot;<br><br>    private val binder = object : UserInfo.Stub() {<br>        override fun getEmailId(value: String?) {<br>           value?.let {<br>             currentEmail = it<br>             Log.d(&quot;AppB&quot;, &quot;email--:$currentEmail&quot;)<br>           }<br>        }<br>    }<br><br>    override fun onBind(intent: Intent?): IBinder {<br>      return binder<br>    }<br><br>}</pre><p>3️⃣ Register the Service in AndroidManifest.xml</p><pre>&lt;service<br>    android:name=&quot;.appb.services.UserInfoService&quot;<br>    android:enabled=&quot;true&quot;<br>    android:exported=&quot;true&quot;&gt;<br>    &lt;intent-filter&gt;<br>        &lt;action android:name=&quot;UserInfo_service&quot; /&gt;<br>    &lt;/intent-filter&gt;<br>&lt;/service&gt;</pre><ul><li>android:exported=&quot;true&quot; → allows external apps (AppA) to bind.</li><li>The action name (UserInfo_service) must match the intent used in AppA.</li></ul><h3>🔁 Summary: How It All Connects</h3><ol><li><strong>AppA</strong> defines an interface using AIDL.</li><li><strong>AppB</strong> implements the interface in a Service.</li><li><strong>AppA</strong> binds to the service using a ServiceConnection.</li><li>AppA can now call getEmailId(&quot;value&quot;) on AppB’s service.</li></ol><h3>🔐 Best Practices &amp; Notes</h3><ul><li>Always <strong>clean your project</strong> after creating or editing .aidl files.</li><li>Make sure the <strong>AIDL file and package names match</strong> in both apps.</li><li>Add <strong>security</strong> if needed (like permission checks in the service).</li><li>Use <strong>null checks</strong> for binder interfaces (userInfo) before calling.</li></ul><h3>📚 Want More?</h3><ul><li><a href="https://developer.android.com/guide/components/aidl">Android Official AIDL Docs</a></li><li><a href="https://developer.android.com/guide/components/bound-services">Bound Services in Android</a></li></ul><h3>💬 Wrapping Up</h3><p>AIDL can seem scary at first, but once you break it down — it’s just a contract between two apps. With Kotlin and the right structure, it becomes much easier to manage.</p><p>If you found this helpful, please clap 👏, share 💬, and follow for more Kotlin and Android insights!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=955a3feca4f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Infix Extension Functions in Android Kotlin]]></title>
            <link>https://medium.com/@maniaravind76/infix-extension-functions-in-android-kotlin-40999e8e416e?source=rss-43d969f93daa------2</link>
            <guid isPermaLink="false">https://medium.com/p/40999e8e416e</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[extension-functions]]></category>
            <category><![CDATA[infix-function]]></category>
            <category><![CDATA[android-development]]></category>
            <dc:creator><![CDATA[AravindMani HariKrishnan]]></dc:creator>
            <pubDate>Sun, 16 Mar 2025 09:00:25 GMT</pubDate>
            <atom:updated>2025-03-16T09:30:39.571Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Kotlin’s <strong>concise and expressive syntax</strong> makes coding more readable. <strong>Infix functions</strong>, when paired with <strong>extension functions</strong>, create <strong>clean, intuitive code</strong>.</p><p>🔹 In this article, you’ll learn:</p><ul><li>What <strong>infix functions</strong> are.</li><li>How to <strong>define &amp; use infix extension functions</strong> in Android.</li><li>Practical <strong>real-world use cases</strong> for cleaner Android code.</li></ul><h3>What Are Infix Functions in Kotlin?</h3><p>An <strong>infix function</strong> is a function called <strong>without parentheses or dot notation</strong>, enhancing readability for <strong>natural language-like</strong> operations.</p><h4>Syntax &amp; Requirements:</h4><p>✔ Must be a <strong>member</strong> or <strong>extension</strong> function.<br> ✔ Takes <strong>exactly one parameter</strong>.<br> ✔ Uses the <strong>infix keyword</strong>.</p><pre>infix fun Int.add(value: Int): Int {<br>    return this + value<br>}<br><br>fun main() {<br>    val result = 10 add 5  // Using infix notation<br>    println(result) // Output: 15<br>}</pre><h3>What Are Infix Extension Functions?</h3><p><strong>Extension functions</strong> add functionality to existing classes <strong>without modifying their source code</strong>. Using the <strong>infix keyword</strong>, they become <strong>more expressive and readable</strong>.</p><h3>Defining an Infix Extension Function</h3><p>Here’s an example of an <strong>infix extension function</strong> for the String class:</p><pre>infix fun String.append(suffix: String): String {<br>    return this + suffix<br>}<br><br>fun main() {<br>    val result = &quot;DownLoad&quot; append &quot;SuccessFul&quot;<br>    println(result)  // Output: DownLoad SuccessFul<br>}</pre><p>This function lets us append a string using a <strong>natural language-like</strong> syntax instead of &quot;Download&quot;.append(&quot; SuccessFul&quot;).</p><h3>Using Infix Extension Functions in Android</h3><h3>1. Infix Extension for Toast Messages</h3><p>You can create an infix extension function for Context to simplify Toast messages.</p><pre>import android.content.Context<br>import android.widget.Toast<br><br>infix fun Context.toast(message: String) {<br>    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()<br>}<br><br>// Usage in an Activity or Fragment<br>class MainActivity : AppCompatActivity() {<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.activity_main)<br><br>        this toast &quot;Welcome to My App!&quot;  // More readable than toast(&quot;Welcome to My App!&quot;)<br>    }<br>}</pre><h3>2. Infix Extension for View Visibility</h3><p>Instead of writing view.visibility = View.VISIBLE, you can create a more readable infix function:</p><pre><br>infix fun View.setVisibility(isVisible: Boolean) {<br>    visibility = if (isVisible) View.VISIBLE else View.GONE<br>}<br><br>// Usage<br>someView setVisibility true</pre><h3>3. Infix Extension for Boolean Operations</h3><p>You can create an <strong>infix extension function for </strong><strong>Boolean</strong> to simplify conditional logic in your Android Kotlin code.</p><pre>fun isNetworkAvailable(): Boolean {<br>    // Assume we check network connectivity here<br>    return true<br>}<br><br>infix fun Boolean.then(action: () -&gt; Unit) {<br>    if (this) action()<br>}<br><br>// Usage<br>isNetworkAvailable() then {<br>    println(&quot;Fetching data from server...&quot;)<br>}</pre><p>This will fetch data <strong>only if the network is available</strong>.</p><h3>4. Running Code on a Background Thread Using Coroutines</h3><pre>import kotlinx.coroutines.*<br><br>infix fun Boolean.then(action: suspend () -&gt; Unit) {<br>    if (this) {<br>        GlobalScope.launch {<br>            action()<br>        }<br>    }<br>}<br><br>// Usage<br>val isHeavyTaskRequired = true<br><br>isHeavyTaskRequired then {<br>    println(&quot;Running a heavy task in the background...&quot;)<br>    delay(2000) // Simulating delay<br>    println(&quot;Heavy task completed!&quot;)<br>}</pre><p>This will execute the <strong>heavy task only if needed</strong>.</p><h3>Advantages of Infix Extension Functions</h3><p>✅ <strong>Improved Readability</strong> — Makes code resemble natural language.<br>✅ <strong>Less Boilerplate</strong> — Eliminates unnecessary parentheses and dot notation.<br>✅ <strong>More Expressive APIs</strong> — Enables cleaner, intuitive code.</p><h3>When Not to Use Infix Functions</h3><p>❌ <strong>If It Reduces Clarity</strong> — Overuse may make code <strong>harder to understand</strong> for newcomers.<br>❌ <strong>For Complex Functions</strong> — Should only be used for simple, <strong>single-argument</strong> operations.<br>❌ <strong>If It’s Not a Clear Relationship</strong> — The infix syntax should make logical sense in the context of usage.</p><h3>Final Thoughts</h3><p>Infix extension functions <strong>streamline Kotlin code</strong>, making it <strong>concise and expressive</strong> in Android development. Use them <strong>wisely</strong> to enhance readability without sacrificing clarity.</p><p>💬 Have you used infix functions in your Android projects? Share your experience in the comments! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40999e8e416e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>