<?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 Maheshchakkarwar on Medium]]></title>
        <description><![CDATA[Stories by Maheshchakkarwar on Medium]]></description>
        <link>https://medium.com/@mahesh-chakkarwar?source=rss-7ea97e0e93f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*O8APrzChgpUbBgKY43z1xQ.jpeg</url>
            <title>Stories by Maheshchakkarwar on Medium</title>
            <link>https://medium.com/@mahesh-chakkarwar?source=rss-7ea97e0e93f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:22:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mahesh-chakkarwar/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[Part-5 : Coroutine Cancellation, NonCancellable Coroutine and Exception Handling]]></title>
            <link>https://mahesh-chakkarwar.medium.com/part-5-coroutine-cancellation-noncancellable-coroutine-and-exception-handling-363647deb3b7?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/363647deb3b7</guid>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Sat, 14 Jan 2023 11:38:12 GMT</pubDate>
            <atom:updated>2023-01-14T12:09:29.094Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part-5 : Coroutine Cancellation, NonCancellable Coroutine and Exception Handling</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9WGFQ7y20qegKl3YqDVipQ.png" /><figcaption>Coroutine Cancellation</figcaption></figure><p>In <a href="https://medium.com/@mahesh-chakkarwar/part-3-coroutine-recipe-using-launch-builder-616002082971">Part-3</a> and <a href="https://medium.com/@mahesh-chakkarwar/part-4-coroutine-builder-async-c5ab94940630">Part-4</a> of Series of Coroutine blogs, we have learned how to launch Coroutine using different builders. In this tutorial we will learn about ExceptionHandling and Cancellation of Coroutine in detail.</p><p>As we know CoroutineScope is associated with Coroutine. When you call coroutineScope.cancel() it cancels all Coroutines runs inside it, but when you cancel CoroutineScope you no longer can use same scope to launch Coroutines. Below snippet of code illustrates this;</p><pre> @Test<br>fun coroutine_scope_cancellation() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch {<br>            println(&quot;Coroutine1 Executed&quot;)<br>        }<br>        val job2 = coroutineScope.launch {<br>            println(&quot;Coroutine2 Executed&quot;)<br>        }<br>        joinAll(job1, job2)<br>    }<br>}<br><br>//Output for this snippet of code is<br>//Coroutine1 Executed<br>//Coroutine2 Executed</pre><p>Now, let’s try same snippet of code by cancelling coroutineScope.</p><pre>@Test<br>fun coroutine_scope_cancellation() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch {<br>            println(&quot;Coroutine1 Executed&quot;)<br>        }<br>        coroutineScope.cancel() //Coroutine cancelled<br>        val job2 = coroutineScope.launch {<br>            println(&quot;Coroutine2 Executed&quot;)<br>        }<br>        joinAll(job1, job2)<br>    }<br>}<br><br>//Output for this snippet of code is<br>//Coroutine1 Executed</pre><p>Here, in above example Coroutine-2 will not be executed as CoroutineScope is no longer valid as scope has already cancelled.</p><p>When Coroutine cancelled as effect of <strong>coroutineScope.cancel(), </strong>it throws CancellationException. Below snippet of code illustrates CancellationException thrown by Coroutine.</p><pre> @Test<br>fun coroutine_scope_cancellation_throws_cancellation_exception() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch {<br>            try {<br>                delay(50)<br>                println(&quot;Coroutine1 Executed&quot;)<br>            } catch (e: CancellationException) {<br>                println(&quot;Coroutine1 Cancelled&quot;)<br>            }<br>        }<br>        coroutineScope.cancel()<br>        joinAll(job1)<br>    }<br>}<br><br>//Output for this snippet of code is<br>//Coroutine1 Cancelled</pre><p>The above example throws <strong>CancellationException, </strong>it prints<strong> “Coroutine1 Cancelled”. </strong>If you see there is no such code except print statement, how it checks Coroutine cancelled? There is <strong>delay() </strong>function which checks Job of the coroutine is completed or cancelled and throws<strong> CancellationException.</strong></p><p>Instead of delay we can check whether Job is cancelled or active by using method <strong>ensureActive()</strong> on Job or CoroutineScope. Below is an example;</p><pre>@Test<br>fun coroutine_scope_cancellation_throws_cancellation_exception() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch {<br>            try {<br>                Thread.sleep(200)<br>                this.ensureActive()<br>                println(&quot;Coroutine1 Executed&quot;)<br>            } catch (e: CancellationException) {<br>                println(&quot;Coroutine1 Cancelled&quot;)<br>            }<br>        }<br>        coroutineScope.cancel()<br>        joinAll(job1)<br>    }<br>}<br>//Output for this snippet of code is<br>//Coroutine1 Cancelled</pre><p>Above example throws <strong>CancellationException, ensureActive() </strong>method checks Coroutine is active or not and if not active throws CancellationException.</p><p>If any <strong>Job</strong> inside <strong>CoroutineScope</strong> is failed, it cancels CoroutineScope and all Jobs associated with that CoroutineScope. Below is an example illustrates failure of job;</p><pre>@Test<br>fun coroutine_job_fails() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch {<br>            try {<br>                delay(50) // throws cancellation exception due to job2 failed<br>                println(&quot;Coroutine1 Executed&quot;)<br>            } catch (e: CancellationException) {<br>                println(&quot;Coroutine1 Cancelled&quot;) <br>            }<br>        }<br>        val job2 = coroutineScope.launch {<br>            throw IllegalArgumentException() //throws exception leads to failure<br>        }<br>        joinAll(job1, job2)<br>        println(job1)<br>        println(job2)<br>        println(coroutineScope)<br>    }<br>}<br><br>//Output:<br>/*Exception in thread &quot;DefaultDispatcher-worker-2 @Coroutine-2#3&quot; java.lang.IllegalArgumentException<br>at com.techyourchance.coroutines.demonstrations.async.AsyncCoroutineBuilderDemoTest$coroutine_job_fails$1$job2$1.invokeSuspend(AsyncCoroutineBuilderDemoTest.kt:149)<br> at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)<br> at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)<br>Coroutine1 Cancelled<br>&quot;Coroutine-1#2&quot;:StandaloneCoroutine{Cancelled}@467a16ae<br>&quot;Coroutine-2#3&quot;:StandaloneCoroutine{Cancelled}@50fb564b<br>CoroutineScope(coroutineContext=JobImpl{Cancelled}@71645b8e)/*</pre><p>If you see output of above snippet of code, job2 throws <strong>IllegalArgumentException. </strong>This leads to failure of job2 which also cancels job1 and job associated with coroutineScope.</p><p>If you want to avoid cancellation of CoroutineScope and other Coroutines, you can use <strong>SupervisorJob</strong> instead of <strong>Job</strong>.</p><blockquote>“<em>A failure or cancellation of a child does not cause the supervisor job to fail and does not affect its other children, so a supervisor can implement a custom policy for handling failures of its children</em>”</blockquote><p>Below is illustrated usage of <strong>SupervisorJob</strong>;</p><pre>@Test<br>fun coroutine_job_fails_not_impacting_coroutine_scope() {<br>    runBlocking {<br>        val scopeJob = SupervisorJob()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch(CoroutineName(&quot;Coroutine-1&quot;)) {<br>            try {<br>                delay(50)<br>                println(&quot;Coroutine1 Executed&quot;) //Coroutine will executed as SupervisorJob is used<br>            } catch (e: CancellationException) {<br>                println(&quot;Coroutine1 Cancelled&quot;)<br>            }<br>        }<br>        val job2 = coroutineScope.launch(CoroutineName(&quot;Coroutine-2&quot;)) {<br>            throw IllegalArgumentException() //fails job2 due to exception not handled<br>        }<br>        joinAll(job1, job2)<br>        println(job1)<br>        println(job2)<br>        println(coroutineScope)<br>    }<br>}<br><br>//Output:<br>/* Exception in thread &quot;DefaultDispatcher-worker-2 @Coroutine-2#3&quot; java.lang.IllegalArgumentException<br> at com.techyourchance.coroutines.demonstrations.async.AsyncCoroutineBuilderDemoTest$coroutine_job_fails$1$job2$1.invokeSuspend(AsyncCoroutineBuilderDemoTest.kt:149)<br> at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)<br> at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)<br>Coroutine1 Executed<br>&quot;Coroutine-1#2&quot;:StandaloneCoroutine{Completed}@34192642<br>&quot;Coroutine-2#3&quot;:StandaloneCoroutine{Cancelled}@467a16ae<br>CoroutineScope(coroutineContext=SupervisorJobImpl{Active}@50fb564b)*/</pre><p>If you see output of above snippet of code, job2 fails but job1 is completed and not thrown CancellationException so not Cancelled. Also, CoroutineScope is active.</p><p>If you want to run Coroutine even if CoroutineScope is cancelled. Then you can use <strong>NonCancellable </strong>while launching Coroutine. Below is defined usage;</p><pre>@Test<br>fun coroutine_job_fails_not_impacting_coroutine_scope() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val job1 = coroutineScope.launch(CoroutineName(&quot;Coroutine-1&quot;) + NonCancellable) {<br>            try {<br>                delay(50)<br>                println(&quot;Coroutine1 Executed&quot;)<br>            } catch (e: CancellationException) {<br>                println(&quot;Coroutine1 Cancelled&quot;)<br>            }<br>        }<br>        val job2 = coroutineScope.launch(CoroutineName(&quot;Coroutine-2&quot;)) {<br>            throw IllegalArgumentException()<br>        }<br>        joinAll(job1, job2)<br>        println(job1)<br>        println(job2)<br>        println(coroutineScope)<br>    }<br>}<br><br>//Output:<br>/* Exception in thread &quot;DefaultDispatcher-worker-2 @Coroutine-2#3&quot; java.lang.IllegalArgumentException<br> at com.techyourchance.coroutines.demonstrations.async.AsyncCoroutineBuilderDemoTest$coroutine_job_fails_not_impacting_coroutine_scope$1$job2$1.invokeSuspend(AsyncCoroutineBuilderDemoTest.kt:149)<br> at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)<br> at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)<br> at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)<br>Coroutine1 Executed<br>&quot;Coroutine-1#2&quot;:StandaloneCoroutine{Completed}@3405fbef<br>&quot;Coroutine-2#3&quot;:StandaloneCoroutine{Cancelled}@b99ba2<br>CoroutineScope(coroutineContext=JobImpl{Cancelled}@540dfb1b)*/</pre><p>In the above example if you see output, you understood that even Coroutine-2 is failed, Coroutine-1 runs successfully. I have passed <strong>NonCancellable</strong> to Coroutine-1 while launching in CoroutineScope.</p><p>In this tutorial you have learned, CancellationException in Coroutine, NonCancellable Coroutine, Supervisor Job vs Job and Exception handling.</p><p>To Discuss more about this topic you can connect me on my <a href="https://in.linkedin.com/in/mahesh-chakkarwar-4b9b6a29?original_referer=https%3A%2F%2Fwww.google.com%2F">linkedIn account </a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=363647deb3b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part-4 : Coroutine Builder Async]]></title>
            <link>https://mahesh-chakkarwar.medium.com/part-4-coroutine-builder-async-c5ab94940630?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5ab94940630</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Sat, 07 Jan 2023 16:12:11 GMT</pubDate>
            <atom:updated>2023-01-14T11:43:40.591Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part-4 : Coroutine async Builder</h3><p>Async Coroutine builder creates Coroutine and return future result object as an implementation of Deferred. The usage of this builder is as below;</p><pre>@Test<br>fun async_builder_usage() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val deferred = coroutineScope.async {<br>            println(&quot;Running Asynchronous Operation&quot;)<br>            delay(100)<br>            500<br>        }<br>        val result = deferred.await()<br>        println(&quot;Result-$result&quot;)<br>    }<br>}</pre><p>The above snippet of code print below output;</p><p><strong>Output</strong>:</p><p>Running Asynchronous Operation</p><p>Result-500</p><blockquote>Async builder returns instance of instance of Deferred, <strong>await</strong>, <strong>awaitAll</strong> and <strong>join</strong> operation needs to call on deferred instance to execute async block. Deferred nothing but implementation of Job. Functions await and join are suspending functions needs to be called from other Coroutine or suspending function.</blockquote><p>When you call cancel on deferred object, cancels async block. Below snippet of code demonstrate cancellation of async block.</p><pre>@Test<br>fun async_block_cancel() {<br>    runBlocking {<br>        val scopeJob = Job()<br>        val coroutineScope = CoroutineScope(scopeJob)<br>        val deferred = coroutineScope.async {<br><br>            println(&quot;Running Asynchronous Operation&quot;)<br>            delay(100)<br>            500<br>        }<br>        coroutineScope.launch {<br>            deferred.cancel() //cancelled async <br>        }<br>        try {<br>            val result = deferred.await() //this will throw cancellation exception <br>            println(&quot;Result-$result&quot;)<br>        } catch (e: CancellationException) {<br>            println(&quot;Async block cancelled&quot;)<br>            println(&quot;${deferred.getCompletionExceptionOrNull()}&quot;)<br>        }<br>    }<br>}</pre><p>Let’s see implementation of async Coroutine Builder;</p><pre>public fun &lt;T&gt; CoroutineScope.async(<br>    context: CoroutineContext = EmptyCoroutineContext,<br>    start: CoroutineStart = CoroutineStart.DEFAULT,<br>    block: suspend CoroutineScope.() -&gt; T<br>): Deferred&lt;T&gt; {<br>    val newContext = newCoroutineContext(context)<br>    val coroutine = if (start.isLazy)<br>        LazyDeferredCoroutine(newContext, block) else<br>        DeferredCoroutine&lt;T&gt;(newContext, active = true)<br>    coroutine.start(start, coroutine, block)<br>    return coroutine<br>}</pre><p><strong>Output:</strong></p><p>Running Asynchronous Operation<br>Async block cancelled<br>kotlinx.coroutines.JobCancellationException: DeferredCoroutine was cancelled; job=”coroutine#2&quot;:DeferredCoroutine{Cancelled}<a href="http://twitter.com/26efe8da">@26efe8da</a></p><blockquote>Parameters to async builder are coroutineContext, StartStrategy and Coroutine block. Here, <strong>StartStrategy</strong> defines four values as fallows;</blockquote><ol><li><strong>Default </strong>— Immediately schedules Coroutine for execution, if cancelled before start, Coroutine will not start but completes with CancellationException</li><li><strong>Lazy</strong> — Starts Coroutine lazily and if cancelled before start, Coroutine will not start but completes with CancellationException</li><li><strong>Atomic</strong> — Schedules Coroutine according to its Context but cannot be cancelled before start</li><li><strong>Undispatched</strong> — Starts Coroutine even if it was cancelled but called in the same Thread</li></ol><blockquote>You can execute multiple concurrent operation using multiple async blocks and combine result using <strong>awaitAll()</strong> function. Below is the example of usage of awaitAll function.</blockquote><pre> @Test<br>    fun concurrent_execution_async_tasks_combineResult() {<br>        runBlocking {<br>            val scopeJob = Job()<br>            val coroutineScope = CoroutineScope(scopeJob)<br>            val sum = (0..5).toList().map {<br>                coroutineScope.async {<br>                    it<br>                }<br>            }.awaitAll().sum()<br>            println(&quot;Sum:-$sum&quot;)<br>        }<br>    }</pre><p>Output: Result:-15</p><p>This is all about async Coroutine builder. In the next part we will learn Exception handling in Coroutine and Cancellable/Non Cancellable Coroutine.</p><p>In the next blog we will learn about ExceptionHandling, CoroutineScope cancellation, NonCancellable Coroutines.</p><p>To Discuss more about this topic you can connect me on my <a href="https://in.linkedin.com/in/mahesh-chakkarwar-4b9b6a29?original_referer=https%3A%2F%2Fwww.google.com%2F">linkedIn account </a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5ab94940630" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part-3 : Coroutine Recipe using Launch Builder]]></title>
            <link>https://mahesh-chakkarwar.medium.com/part-3-coroutine-recipe-using-launch-builder-616002082971?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/616002082971</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[coroutinescope]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[suspend]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Fri, 06 Jan 2023 04:13:05 GMT</pubDate>
            <atom:updated>2023-01-07T16:14:09.512Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part-3 : Coroutine Recipe using Launch Builder</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*YILk4RKVa6SkyLBZv5OCuQ.png" /></figure><p>In the last part <a href="https://medium.com/@mahesh-chakkarwar/part-2-blocking-thread-vs-suspending-coroutines-d978f14b383b">Part-2</a> we have learned how the coroutines differentiates from JVM Thread. In this blog we will learn how to create Coroutine using Coroutine Builder to achieve <a href="https://medium.com/@mahesh-chakkarwar/mastering-coroutines-for-android-development-part1-629bd607660a">Structured Concurrency</a>. Before creation of Coroutine one need to understand <a href="https://medium.com/@mahesh-chakkarwar/mastering-coroutines-for-android-development-part1-629bd607660a">CoroutineScope</a>, <a href="https://medium.com/@mahesh-chakkarwar/mastering-coroutines-for-android-development-part1-629bd607660a">CoroutineContext</a>, <a href="https://medium.com/@mahesh-chakkarwar/mastering-coroutines-for-android-development-part1-629bd607660a">Job</a>.</p><p>We have learned CoroutineScope is nothing but wrapper around CoroutineContext. CoroutineScope waits until all children Coroutines running inside CoroutinesScope not executed.</p><p>CoroutineScope provides two factory functions for creation of CoroutineScope;</p><blockquote><strong>CoroutineScope</strong> : One has to pass Job instance to CoroutineScope factory function if not provided, it will create Job. See below CoroutineScope factory function. If any children Coroutine fails it fails CoroutineScope in return cancels all children Coroutines of scope.</blockquote><pre>public fun CoroutineScope(context: CoroutineContext): CoroutineScope =<br>    ContextScope(if (context[Job] != null) context else context + Job())</pre><blockquote><strong>MainScope</strong> : MainScope has SupervisorJob and runs on Main Dispatcher. So, it is used run operation on Main/UI Thread. This uses Supervisor Job, it means even it Any children fail Coroutine scope will not fail and cancels Coroutines.</blockquote><pre>public fun MainScope(): CoroutineScope = ContextScope(SupervisorJob() + Dispatchers.Main)</pre><blockquote>GlobalScope : Global scope is used to launch top-level Coroutines which are operating at application level. It is discouraged to use GlobalScope. There is no Job associated with GlobalScope.</blockquote><pre>public object GlobalScope : CoroutineScope {<br>    /**<br>     * Returns [EmptyCoroutineContext].<br>     */<br>    override val coroutineContext: CoroutineContext<br>        get() = EmptyCoroutineContext<br>}</pre><p>CoroutineContext is nothing but includes Job that executes Coroutine. It contains set consisting Unique keys like Job, CoroutineName, Dispatcher. You can use plus operator to add CoroutineName, Dispatcher and Job. You can keys as below;</p><pre>val scopeJob = SupervisorJob()<br>val scope = CoroutineScope(CoroutineName(&quot;Coroutine-1&quot;) + scopeJob + Dispatchers.Default)<br>scope.coroutineContext[Job]<br>scope.coroutineContext[CoroutineName]</pre><p>You can launch using launch builder as below;</p><pre>@Test<br>fun test_coroutine() {<br>    runBlocking {<br>        val job = Job()<br>        val coroutineScope = CoroutineScope(job)<br>        val job = coroutineScope.launch {<br>            println(&quot;Inside Coroutine&quot;)<br>        }<br>        joinAll( job)<br>    }<br>}</pre><p>If you have to launch Coroutine within another Coroutine you can use withContext function. withContext is suspending function which executes suspending block with a given CoroutineContext, suspends until execution completes.</p><pre>@Test<br>fun test_coroutine() { <br>    runBlocking {<br>        val job = coroutineScope.launch {<br>        println(&quot;Inside Coroutine&quot;)<br>        //this coroutine suspends until this withContext block not executed<br>        withContext(job) {<br>        println(&quot;Inside Inside Coroutine&quot;)<br>        }<br>    }<br>    joinAll(job)<br>    }<br>}</pre><blockquote>Here, to achieve structured concurrency we need to use joinAll function. This function suspends run-blocking Coroutine until Coroutine inside it not completes execution.</blockquote><p>Cancellation of CoroutineScope will cancel all coroutines inside that scope. When you call cancel on CoroutineScope it will cancel Job associated with it. So, you no longer will be able to launch new Coroutine using that CoroutineScope. Below is the example;</p><pre>@Test<br>fun test_cancel_coroutine() {<br>  runBlocking {<br>      val job = Job()<br>      val coroutineScope = CoroutineScope(job)<br>      val job2 = coroutineScope.launch {<br>          println(&quot;Inside Coroutine-1&quot;)<br>          //cancel function on coroutineScope cancels all coroutines lunched inside it<br>          coroutineScope.cancel()<br>          withContext(job) {<br>              println(&quot;Inside Coroutine-2&quot;)<br>          }<br>      }<br>      joinAll( job2)<br>  }<br>}</pre><p>In the above snippet of code, Inside Coroutine-1 only will be printed. It will not execute withContext suspending block as coroutineScope.cancel() function cancels all coroutines.</p><p>If you want to execute withContext suspending block even if outer coroutine cancels below are the modifications.</p><pre>@Test<br>fun test_cancel_coroutine() {<br>    runBlocking {<br>        val job = Job()<br>        val coroutineScope = CoroutineScope(job)<br>        val job2 = coroutineScope.launch {<br>            println(&quot;Inside Coroutine-1&quot;)<br>            cancel() //cancel here cancels outer coroutine but withContext executes with scopeJob  <br>            withContext(job) {<br>                println(&quot;Inside Coroutine-2&quot;)<br>            }<br>        }<br>        joinAll( job2)<br>    }<br>}</pre><p>This prints both lines “Inside Coroutine-1” and “Inside Coroutine-2”.</p><p>Let’s modify above code to understand structured concurrency with withContext suspending block.</p><pre>@Test<br>fun test_withContext_block() {<br>    runBlocking {<br>        val job = Job()<br>        val coroutineScope = CoroutineScope(job)<br>        val job2 = coroutineScope.launch {<br>            println(&quot;Inside Coroutine&quot;)<br>            withContext(job) {<br>                delay(150)<br>                println(&quot;Inside Inside Coroutine&quot;)<br>            }<br>            println(&quot;Coroutine finishing&quot;)<br>        }<br>        joinAll(job2)<br>    }<br>}</pre><p>When above snippet of code executed below is the output;</p><p><strong>Output:</strong></p><p><em>Inside Coroutine</em></p><p><em>Inside Inside Coroutine</em></p><p><em>Coroutine finishing</em></p><p>This means that withContext is suspends the outer coroutine until it not finishes execution.</p><p>Suspending function is spatial type of function invoked in Coroutine or another suspending function. Parent Coroutine or caller suspending function will be suspended until the execution of called suspending function. Suspending function can suspend entering in waiting state without blocking the calling Thread. Suspending function need not to be run on same Dispatcher as caller, one can switch Dispatcher while creation.</p><p>Let’s try to modify above code to see usage of suspend function.</p><pre>@Test<br>fun test_suspending_function() {<br>    suspend fun suspendExecuteOnBackground() = withContext(Dispatchers.IO) {<br>        delay(150)<br>        println(&quot;Inside Coroutine&quot;)<br>    }<br>  <br>    runBlocking {<br>        val job = Job()<br>        val coroutineScope = CoroutineScope(job)<br>        val job2 = coroutineScope.launch {<br>            println(&quot;Inside Inside Coroutine&quot;)<br>            suspendExecuteOnBackground()<br>            println(&quot;Coroutine finishing&quot;)<br>        }<br>        joinAll(job2)<br>    }<br>}</pre><p>This will print same output as the previous example, only thing is that suspending block is executing in IO dispatcher.</p><p>Thank you for Reading this block. Hope this will really help to understand Coroutine Recipe using launch builder😁.</p><p>In the next part <a href="https://mahesh-chakkarwar.medium.com/part-4-coroutine-builder-async-c5ab94940630">Part-4</a> we will learn async Coroutine builder.</p><p>To Discuss more about this topic you can connect me on my <a href="https://in.linkedin.com/in/mahesh-chakkarwar-4b9b6a29?original_referer=https%3A%2F%2Fwww.google.com%2F">linkedIn account </a>.</p><p>— — — — — — — — — — — — — — — “Sharing is Caring” — — — — — — — — — — — — —</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=616002082971" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part-2 : Blocking Thread vs Suspending Coroutines]]></title>
            <link>https://mahesh-chakkarwar.medium.com/part-2-blocking-thread-vs-suspending-coroutines-d978f14b383b?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d978f14b383b</guid>
            <category><![CDATA[suspend]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[blocking]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Sat, 31 Dec 2022 14:55:36 GMT</pubDate>
            <atom:updated>2023-01-06T04:22:08.866Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part-2 : Blocking Thread vs Suspending Coroutines</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gZRqegszMXa9ZzeieaDEhA.png" /><figcaption>Coroutines Vs Thread</figcaption></figure><p>In the last <a href="https://medium.com/@mahesh-chakkarwar/mastering-coroutines-for-android-development-part1-629bd607660a">Part-1</a>, we have learned basics of Coroutines, building blocks of Coroutines. Here, you will learn difference between Java Thread and Coroutines. We call Coroutines as non blocking way of execution i.e Thread on which it is running is not blocked.</p><blockquote>First try to understand, blocking Thread means suspending execution allows processor to run other Threads. We use Thread.sleep(100) method to suspend execution of Thread. Let’s try with an example;</blockquote><pre>@Test<br>fun test_block_thread() {<br>    val thread1 = Thread(&quot;Thread-1&quot;)<br>    thread1.run {<br>        println(System.currentTimeMillis()/1000)<br>        println(this.name)<br>    }<br>    val thread2 = Thread(&quot;Thread-2&quot;)<br>    thread2.run {<br>        println(System.currentTimeMillis()/1000)<br>        println(this.name)<br>    }<br>    thread1.start()<br>    thread2.start()<br>}</pre><blockquote>Here, I have created two Threads, both Threads are started at same time. So, the execution of both these Threads are executed in parallel on Main Thread. So, output is as below;</blockquote><p><strong>Output</strong> : 1672496481<br> Thread-1<br>1672496481<br>Thread-2</p><p>If you look at the timestamp both threads are executed in parallel on Main Thread, due to which timestamp is same.</p><p>Now, try same example by putting Sleep(10000) in Thread1. Below is the snippet of code after adding Sleep.</p><pre>@Test<br>fun test_block_thread() {<br>    val thread1 = Thread(&quot;Thread-1&quot;)<br>    thread1.run {<br>        println(System.currentTimeMillis()/1000)<br>        println(this.name)<br>        Thread.sleep(10000) //sleep added<br>    }<br>    val thread2 = Thread(&quot;Thread-2&quot;)<br>    thread2.run {<br>        println(System.currentTimeMillis()/1000)<br>        println(this.name)<br>    }<br>    thread1.start()<br>    thread2.start()<br>}</pre><p><strong>Output: </strong>1672496481<br> Thread-1<br>1672496491<br>Thread-2</p><p>As the main Thread1 is blocked for 10 sec, execution of Thread2 is postponed for 10 sec. This is called blocking or suspending.</p><blockquote>Now, let’s try same example using Coroutines, instead of creating two Threads, create two Coroutines.</blockquote><pre>@Test<br>fun test_suspending_coroutines() {<br>  runBlocking {<br>      launch(CoroutineName(&quot;Coroutine-1&quot;)) {<br>          println(System.currentTimeMillis() / 1000)<br>          println(&quot;Coroutine-1&quot;)<br>      }<br>      launch(CoroutineName(&quot;Coroutine-2&quot;)) {<br>          println(System.currentTimeMillis() / 1000)<br>          println(&quot;Coroutine-2&quot;)<br>      }<br>  }<br>}</pre><p>Both Coroutines will start at same time. Timestamp printed will be same.</p><p><strong>Output:</strong> 1672497810<br>Coroutine-1<br>1672497810<br>Coroutine-2</p><p>Let’s try by putting delay(10000) suspending function in first Coroutine.</p><pre>fun test_suspending_coroutines() {<br>        runBlocking {<br>            launch(CoroutineName(&quot;Coroutine-1&quot;)) {<br>                println(System.currentTimeMillis() / 1000)<br>                println(&quot;Coroutine-1&quot;)<br>                delay(10000)<br>            }<br>            launch(CoroutineName(&quot;Coroutine-2&quot;)) {<br>                println(System.currentTimeMillis() / 1000)<br>                println(&quot;Coroutine-2&quot;)<br>            }<br>        }<br>    }</pre><p>Output: 1672498311<br>Coroutine-1<br>1672498311<br>Coroutine-2</p><p>If you observed timestamp is same. We understood from this, Coroutines delay function suspends execution of current Coroutine but not blocks execution of Thread.</p><p>To know about creation of Coroutine using builder, read my blog <a href="https://mahesh-chakkarwar.medium.com/part-3-coroutine-recipe-using-launch-builder-616002082971">Coroutine Recipe Part-3</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d978f14b383b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Coroutines for Android Development -Part1]]></title>
            <link>https://mahesh-chakkarwar.medium.com/mastering-coroutines-for-android-development-part1-629bd607660a?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/629bd607660a</guid>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[coroutine]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Sat, 31 Dec 2022 08:01:18 GMT</pubDate>
            <atom:updated>2023-01-01T15:49:38.920Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I have completed Masterclass of Coroutines, I thought I should make easy learning for others by creating stuffs. I will create series of Coroutine learnings stufs. Here, I am explaining Coroutines and usage in Android.</p><p>Coroutines is asynchronous way of writing steps of execution, have the capability suspending instead of blocking thread. It is lightweight to run multiple coroutines on same Thread.</p><blockquote>Coroutines has the ability of structured concurrency, it means pause execution and wait for concurrent flows to complete execution.</blockquote><p>There are four building blocks of coroutines;</p><ol><li>Coroutine Dispatcher</li><li>Coroutine Scope</li><li>Coroutine Job</li><li>Coroutine Builders</li></ol><p>Coroutine Dispatcher executes steps on specific threads or group of threads. Also, single coroutine can execute on multiple dispatchers. Dispatchers is nothing but pool of threads, the code inside these coroutines executes on these Threads.</p><p>There are three types of Coroutine Dispatchers in Android;</p><ol><li><strong>Dispatchers.Main</strong> : Dispatcher that is confined to Main thread that performs UI operations. It is in general runs on single thread instead of pool of threads. It is similar to Handler(Looper.getMainLooper()).post{} which runs on another looper. So, redispaching execution will happen.</li><li><strong>Dispatchers.Main.Immediate</strong> : Dispatcher that executes Coroutines immediately. If immediate dispatching not supporting it throws Unsupported Exception. This is similar to runOnUiThread method in Activity i.e. it runs on same looper as current looper in Activity, so no dispatching is required. This support immediate execution for Android, JavaFx and Swing platforms.</li><li><strong>Dispatchers.Default :</strong> It is backed by shared pool of threads on JVM. The number of threads in thread pool equivalent to no of CPU cores or minimum of 2.</li><li><strong>Dispatchers.IO : </strong>This is designed for blocking IO tasks. This is backed by capping of 64 threads or number of CPU cores. Thread pool is shared with default dispatchers. So, for nested coroutines if you have not specified Dispatcher execution will continue on same Dispatcher.</li><li><strong>Dispatchers.Unconfined </strong>: This is not confined to any specific thread. Execution continue in same thread as caller thread. But, the order of execution is not guaranteed, as jobs are queued and executed in any order. For example, below snippet of code will print both “1 2 3” or “1 3 2”</li></ol><pre>withContext(Dispatchers.Unconfined) {<br>   println(1)<br>   withContext(Dispatchers.Unconfined) {<br>       println(2)<br>   }<br>   println(3)<br>}<br>println(&quot;Done&quot;)</pre><p>Coroutine Scope is like as box which runs all coroutines. CoroutineScope() and MainScope() are the two scope factory functions. This is wrapper around Coroutine Context. If the Job not provided by context it creates default Job() to run Coroutines. If you cancelled scope it will cancel all routines launched under CoroutineScope(). Below is a way to create CoroutineScope;</p><pre>val scopeJob = Job()<br>val scope = CoroutineScope(scopeJob)</pre><p>CoroutineBuilder launches new Coroutine without blocking current thread. It inherits CoroutineContext from CoroutineScope. There are below ways to launch Coroutine using CoroutineBuilders;</p><ol><li><strong>runblocking Builder :</strong> Runs new Coroutine and blocks current thread of execution until new Coroutine finishes. This is not to be used inside Coroutines. This is specifically designed for Main method and Test methods to test Coroutine behaviour. If you pass CoroutineDispatcher to this builder, the new Coroutine runs inside CoroutineDispatcher but the thread is blocked until Coroutine completion.</li></ol><pre>val scopeJob=Job()<br>runBlocking(scopeJob) {<br>   //Launched Coroutine inside this builder will be executed by scopeJob <br>   //but the thread which calls runBlocking blocked until coroutine completes<br>}</pre><ol><li><strong>launch Builder : </strong>Launches new coroutine without blocking current thread. This returns reference to coroutine as a Job. The CoroutineContext inherited from CoroutineScope.</li></ol><pre>val scopeJob = Job()<br>val coroutineScope = CoroutineScope(scopeJob)<br>val job = coroutineScope.launch {<br>    //snippet of this code to run inside this coroutine<br>}</pre><ol><li><strong>async Builder : </strong>Creates Coroutine and returns result as an implementation of Deferred. The CoroutineContext inherited from CoroutineScope.</li></ol><pre>val scopeJob = Job()<br>val coroutineScope = CoroutineScope(scopeJob)<br>val deferred = coroutineScope.async {<br>    //snippet of this code to run inside this coroutine returns some value<br>}<br>val returned = deferred.await()</pre><p>Job does not return result value. Jobs are launched solely for their side-effects. The job has following states;</p><ol><li>start : Coroutine is started</li><li>isActive : Coroutine is still active</li><li>isCompleted : Coroutine completed</li><li>isCancelled : Coroutine cancelled</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v27srOSM4AzC0EJPF-hYrA.png" /><figcaption><strong>Coroutine Job lifecycle</strong></figcaption></figure><p>To understand suspending Coroutines over blocking Thread find my next blog <strong>Part-2</strong> (<a href="https://mahesh-chakkarwar.medium.com/part-2-blocking-thread-vs-suspending-coroutines-d978f14b383b">Suspending Coroutines vs Blocking Thread</a>).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=629bd607660a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android: Difference between runOnUiThread Vs Handler.post()]]></title>
            <link>https://mahesh-chakkarwar.medium.com/difference-between-runonuithread-vs-handler-post-ed14996ac15d?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed14996ac15d</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Wed, 09 Nov 2022 15:21:22 GMT</pubDate>
            <atom:updated>2022-11-09T15:21:41.590Z</atom:updated>
            <content:encoded><![CDATA[<p>You must have used runOnUiThread, this function is present in Activity class. This is used for running block of code on MainThread, as in Android all components like Activity, Fragment, Services etc. runs on MainThread.</p><p>Method runOnUiThread take runnable as parameter that it runs on MainThread. It runs immediately on Main thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0afdbf20c87982d0d27784b2be72fe7/href">https://medium.com/media/c0afdbf20c87982d0d27784b2be72fe7/href</a></iframe><p>Another way is to using Handler that is linked to Main thread using MainLooper.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14f01ef1e46d2aceeb5480d043712b11/href">https://medium.com/media/14f01ef1e46d2aceeb5480d043712b11/href</a></iframe><p>This will post runnable to queue which will be executed by MainThread.</p><p>So, difference is when you execute block of code using runOnUiThread, it will be executed immediately. While when you execute Handler.post(), it will execute when its turn comes in queue.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed14996ac15d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Share large data over 1MB using Intent App chooser/Email chooser]]></title>
            <link>https://mahesh-chakkarwar.medium.com/share-large-data-over-1mb-using-intent-appchooser-5f606a58fd6a?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f606a58fd6a</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[data-sharing]]></category>
            <category><![CDATA[appchooser]]></category>
            <category><![CDATA[emailchooser]]></category>
            <category><![CDATA[uri]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Wed, 09 Nov 2022 14:57:12 GMT</pubDate>
            <atom:updated>2022-11-09T15:00:03.279Z</atom:updated>
            <content:encoded><![CDATA[<p>I was looking to share a file through Intent Chooser. We share data using IntentChooser through bundle. Refer below source code for sharing data using IntentChooser;</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92c332ef430b1887d0a8d906f471fcf7/href">https://medium.com/media/92c332ef430b1887d0a8d906f471fcf7/href</a></iframe><p>Here, app chooser screen appear which shows all apps like email client, bluetooth etc.</p><p>Here, if you see data is sent using intent, you cannot send data over 1MB. So, the approach you can use save file to external/internal storage first and share data using android.net.Uri via intent. As you are sharing file using Uri, you need to add provider in <strong>AndroidManifest.xml</strong> Below is source code;</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/639631aae173a655996c9bfc55796293/href">https://medium.com/media/639631aae173a655996c9bfc55796293/href</a></iframe><p>Hope, this will helpful to you if you are looking for sharing data using EmailChooser or App chooser.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f606a58fd6a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sonarqube integration in multimodule Android Application]]></title>
            <link>https://mahesh-chakkarwar.medium.com/sonarqube-integration-in-multimodule-android-application-13e03e01e70b?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/13e03e01e70b</guid>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Tue, 20 Sep 2022 10:42:07 GMT</pubDate>
            <atom:updated>2022-09-20T10:42:07.526Z</atom:updated>
            <content:encoded><![CDATA[<p>I was wondering to integrate Sonarqube code analysis tool for multi-module application, where my application contains multiple independent modules. I want to map all modules under same applicationId in sonarqube dashboard. After lots of trials and learning, finally I am succeed.</p><p>Below image displaying the project structure, having app module and library module.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pb6tupvoZU2yA8mpTGDxmQ.png" /><figcaption>Sample application for Sonarqube integration</figcaption></figure><p>What I want to achieve?</p><p>I want to display all modules under single project in sonarqube dashboard. Below screenshot displays maps app and libraryModule under SonarSample project on sonar dashboard;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s7Ufk4L4yOIxTaGz7JHpmQ.png" /><figcaption>Sonarqube code hierarchy</figcaption></figure><p>I have written sub-projects block inside top level global build.gradle to define common sonar properties for all modules. Also, here I have defined all sonar properties in top level gradle. I have defined <strong>sonarqube.gradle</strong> file which need to be added into every module build.gradle i.e app level build.gradle and library module gradles as below;</p><pre>apply <strong>from</strong>: <strong>&#39;../sonarqube.gradle&#39;</strong></pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89e9686c4eb9ac38d048deacb2555bb6/href">https://medium.com/media/89e9686c4eb9ac38d048deacb2555bb6/href</a></iframe><p>After this just run below gradle command</p><p>./gradlew sonarqube — stacktrace</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13e03e01e70b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Compose Interoperability with Existing android views]]></title>
            <link>https://mahesh-chakkarwar.medium.com/android-compose-interoperability-with-existing-android-views-663794919932?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/663794919932</guid>
            <category><![CDATA[compose-ui]]></category>
            <category><![CDATA[android-views]]></category>
            <category><![CDATA[existing-android-views]]></category>
            <category><![CDATA[interoperability]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Sun, 17 Jul 2022 06:27:37 GMT</pubDate>
            <atom:updated>2022-07-17T06:37:06.835Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*jst3ST0nFEqed7HEN-xzww.png" /></figure><p>Jetpack compose is a modern declarative UI toolkit. It has several advantages to over traditional UI toolkit. It has advantages like;</p><ol><li>Compose UI toolkit not stores any state inside</li><li>Traditional android View needs to be updated manually by calling setter methods</li><li>Declarative UI simplifies UI engineering and update interface</li><li>Regenerating entire screen is expensive in terms of computing and battery usage</li><li>Recomposition just called by calling composable function with new data</li></ol><p>But changing to new technology is very expensive if try to develop whole completely. No one will choose to completely replace old technology like tradition UI toolkit with modern toolkit like compose. But, if we adopt android compose with slowly and try migrating iteratively is the best way.</p><p>So, we will discuss here how we can adopt compose views with existing android views and vice versa means use already developed custom views in traditional way in Compose UI.</p><p><strong>Adopt ComposeViews in existing AndroidViews i.e xml files</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba4bc3bb93e6e53c3f31acea8c765420/href">https://medium.com/media/ba4bc3bb93e6e53c3f31acea8c765420/href</a></iframe><p>Use AndroidViews in Compose</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8a34afdc11ca5878b1e4ccddb499e07/href">https://medium.com/media/b8a34afdc11ca5878b1e4ccddb499e07/href</a></iframe><p>So, ComposeView provides interoperability with existing Android Views. Just we have to keep in mind dispose composable view by providing ViewCompositionStrategy.</p><p>References</p><ol><li><a href="https://developer.android.com/jetpack/compose/interop/adding">https://developer.android.com/jetpack/compose/interop/adding</a></li><li><a href="https://developer.android.com/jetpack/compose/interop/interop-apis">https://developer.android.com/jetpack/compose/interop/interop-apis</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=663794919932" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implement Popup like Linked reaction popup in Android Compose]]></title>
            <link>https://mahesh-chakkarwar.medium.com/implement-popup-like-linked-reaction-popup-in-android-compose-aaf7cf3112fc?source=rss-7ea97e0e93f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/aaf7cf3112fc</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[compose-ui]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <dc:creator><![CDATA[Maheshchakkarwar]]></dc:creator>
            <pubDate>Sun, 03 Jul 2022 06:27:42 GMT</pubDate>
            <atom:updated>2022-07-03T06:27:42.218Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*D8wNQgRu-r56VH_1JAxOTw.png" /></figure><p>I was looking to implement popup like linkedIn using Android Jetpack compose. After lots of searching I got to know there is Popup in Jetpack Compose. Thanks to Android Developer documentation <a href="https://developer.android.com/reference/kotlin/androidx/compose/ui/window/package-summary"><strong>link</strong></a><strong> .</strong></p><p>Initially I thought it would be straight forward in Jetpack Compose like on tap of button to show popup as below we do in Android old way.</p><p><strong>Android Old way(Without Jetpack Compose):</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17a0f0ea7dfe406038e643dc71ade06e/href">https://medium.com/media/17a0f0ea7dfe406038e643dc71ade06e/href</a></iframe><p><strong>Android New way (With Jetpack Compose)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34f05b96cbf65fe591772f27e036505e/href">https://medium.com/media/34f05b96cbf65fe591772f27e036505e/href</a></iframe><p><strong>Use below code to launch popup on tap of Button</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dfe4407d1429439113656938ae848a1/href">https://medium.com/media/7dfe4407d1429439113656938ae848a1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P5-IYz3JvjZzZp-4WjsgLw.gif" /></figure><p>Hope this article helps everyone who are looking to implement popup like LinkedIn😁.</p><p><strong>References:</strong></p><ol><li><a href="https://developer.android.com/reference/kotlin/androidx/compose/ui/window/package-summary">https://developer.android.com/reference/kotlin/androidx/compose/ui/window/package-summary</a></li><li><a href="https://developer.android.com/reference/android/widget/PopupWindow">https://developer.android.com/reference/android/widget/PopupWindow</a></li><li><a href="https://stackoverflow.com/questions/5944987/how-to-create-a-popup-window-popupwindow-in-android">https://stackoverflow.com/questions/5944987/how-to-create-a-popup-window-popupwindow-in-android</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aaf7cf3112fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>