<?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 Swechha Prakash on Medium]]></title>
        <description><![CDATA[Stories by Swechha Prakash on Medium]]></description>
        <link>https://medium.com/@swechhaprakash?source=rss-85886b3096ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fshdwymsurkZrO2z1LhL4Q.jpeg</url>
            <title>Stories by Swechha Prakash on Medium</title>
            <link>https://medium.com/@swechhaprakash?source=rss-85886b3096ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 23:25:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@swechhaprakash/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[Understanding HandlerThreads on Android]]></title>
            <link>https://medium.com/@swechhaprakash/understanding-handlerthreads-on-android-b7b842ab5cc4?source=rss-85886b3096ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7b842ab5cc4</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Swechha Prakash]]></dc:creator>
            <pubDate>Tue, 07 Jun 2016 11:27:03 GMT</pubDate>
            <atom:updated>2016-06-07T11:27:03.785Z</atom:updated>
            <content:encoded><![CDATA[<p>I was designing an Android application that acts as a server and receives a high volume of requests from its clients. Each of these requests, apart from being just background tasks, also might need to update the UI constantly. This was when I first came across <em>HandlerThreads</em>. HandlerThreads are convenience classes in the Android SDK are especially useful in applications that have many background tasks that need to work with the UI. But they are not adequately documented in terms of how they work internally.</p><p>According to Android Developers documentation-</p><blockquote>Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.</blockquote><p>HandlerThread is a special type of thread that comes with its own <em>Looper</em> and <em>MessageQueue</em>. Whenever a task is posted to a handler using the <em>.post()</em> method, internally the handler simply picks this task up and places it inside a queue. This queue is called the MessageQueue. A Looper’s responsibility is to loop through this MessageQueue and send the tasks for execution. This means that there needs to be a Looper associated with each Handler. To take care of this, whenever you define a new Handler, it associates itself with the Looper of the Thread in which it is defined. But not all threads have loopers, and this is why if you try defining a new Handler in a newly spawned thread, you will most definitely run into an error like this -</p><pre>E/AndroidRuntime(810): FATAL EXCEPTION: Thread-75<br>E/AndroidRuntime(810): java.lang.RuntimeException: Can&#39;t create handler inside thread that has not called Looper.prepare()</pre><p>A call to <em>Looper.prepare()</em> in your new Thread will create a new Looper for your Thread; then you can set up your Looper before you can create new Handlers and post tasks to them. Or you can use a HandlerThread that already takes care of this. The entire process right from submitting a new task to executing it can be pictured somewhat like this -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*-Hdn8U5v3rtZ-93aqnxmgg.png" /></figure><p>So if you break it down -</p><ul><li><strong>MessageQueue</strong> is a queue of messages that need to be executed</li><li><strong>Handler</strong> places new messages in the MessageQueue, and handles the execution of these messages.</li><li><strong>Looper</strong> loops through the MessageQueue and sends the tasks to their respective Handlers for execution</li></ul><p>Because of the presence of a dedicated <em>MessageQueue </em>and <em>Looper</em>, a <em>HandlerThread</em> stays alive expecting more messages to be added to the queue, while a regular thread stays alive only long enough to serve the task that was assigned to it. This property of <em>HandlerThreads</em> makes them a great tool to efficiently handle a large number of tasks.</p><p>The following example shows what a basic HandlerThread class may look like:</p><pre><strong>public</strong> <strong>class</strong> <strong>TaskHandlerThread</strong> <strong>extends</strong> <strong>HandlerThread</strong> { <br>  <strong>private</strong> Handler mTaskHandler; <br>  <br>  <strong>public</strong> <strong>TaskHandlerThread</strong>(String name) { <br>    <strong>super</strong>(name); <br>  } <br>  <br>  <strong>public</strong> <strong>void</strong> <strong>postTask</strong>(Runnable task) { <br>    mTaskHandler.post(task); <br>  } <br>  <br>  <strong>public</strong> <strong>void</strong> <strong>prepareHandler</strong>() {<br>    mTaskHandler = <strong>new</strong> Handler(getLooper()); <br>  } <br>}</pre><p>Here, a new handler called <em>mTaskHandler </em>is initialized in the method <em>prepareHandler</em>, this method must be called before posting any task. Whenever a new task is sent to this HandlerThread using the method <em>postTask(Runnable task)</em>, it is added to the MessageQueue by mTaskHandler. From there onwards, it is the Looper’s responsibility to loop through this MessageQueue and send the task for execution. When instantiating and calling this from an activity, this is what the code will look like -</p><pre><em>//Inside an activity</em><br>@ Override<br><strong>protected void onCreate(</strong>Bundle savedInstanceState<strong>)</strong> {<br>  <strong>super</strong>.onCreate(savedInstanceState);<br>  setContentView(R.layout.activity_main);</pre><pre><strong>  </strong><em>//Initialize HandlerThread and post a task</em><strong><br>  private</strong> TaskHandlerThread mTaskHandlerThread = <strong>new </strong>TaskHandlerThread(&quot;TaskHandlerThread&quot;);<br>  mTaskHandlerThread.start();<br>  mTaskHandlerThread.prepareHandler();</pre><pre><strong>  private </strong>Runnable myRunnable = <strong>new</strong> Runnable() {<br>    @ Override<br>    <strong>public</strong> void run() {<br>      <em>//Main task execution logic here</em><br>    }<br>  }<br>  mTaskHandlerThread.postTask(myRunnable);<br>}</pre><pre>...</pre><pre>@ Override<br><strong>protected</strong> <strong>void</strong> <strong>onDestroy</strong>() {<br>  mTaskHandlerThread.quit();<br>  super.onDestroy();<br>}</pre><p>It is important to explicitly call <em>quit()</em> on the TaskHandlerTask in order to destroy it.</p><p>In this particular example, I have made use of HandlerThread to post tasks in the form of <em>Runnables</em>. Another approach would be to directly post <em>Messages </em>that will be picked up and handled by Handlers, but that is probably a story for another time.</p><p>I hope you found this useful! Feel free to drop a comment or get in touch <a href="https://twitter.com/swechhaprakash">@swechhaprakash</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7b842ab5cc4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Design Shots — Dribbble Client]]></title>
            <link>https://medium.com/@swechhaprakash/design-shots-dribbble-client-6f4658b706a4?source=rss-85886b3096ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f4658b706a4</guid>
            <dc:creator><![CDATA[Swechha Prakash]]></dc:creator>
            <pubDate>Wed, 14 May 2014 19:33:19 GMT</pubDate>
            <atom:updated>2014-05-17T16:47:07.017Z</atom:updated>
            <content:encoded><![CDATA[<h4>v2.1</h4><p><a href="https://itunes.apple.com/us/app/design-shots-dribbble-client/id792517951?mt=8">Design Shots</a> was first released in January 2014, along with some basic features to browse Dribbble and view Shots. This is going to change with version 2.1, that comes loaded with a bunch of new features and UI enhancements.</p><h3>View comments</h3><p>Tap on any Shot to view comments and other details</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*O9Ov3M7yMDrOr8ssrvVqhA.gif" /></figure><h3>User View</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*Eqvgk_ertncoAdY07p9rvQ.gif" /></figure><ul><li>View the Player’s Twitter profile and website by tapping on the icons</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*1bUnc_ujlQkkhqGkFlssmA.gif" /></figure><ul><li>Tap “Likes” to view all shots liked by the Player</li><li>Scroll through all shots posted by the Player and tap on any shot to view details</li><li>Long press on a Shot to quickly share</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*tkeo_PeQ_BPOr_UAMJ9C2g.gif" /></figure><h3>New sharing options</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*wUBRn56QJzp_ZWYV-O_v2g.png" /></figure><h3>Delightful Animations</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*SDCi4xd-ACrfVYrwfgHe_w.gif" /><figcaption>Transition from Shot to Player</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*K8OZooesj26D7hjJ4p5uBA.gif" /><figcaption>Comments on a Shot</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*URN5Scn_EZG7prDQXwsvdA.gif" /><figcaption>Viewing a Shot’s details</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*e_Ly6geWC7gg3ntJ_YpmgQ.gif" /><figcaption>The main menu</figcaption></figure><p>We hope you enjoy this Design Shots update as much as we enjoyed developing it. We’d appreciate it if you leave us a review on the App Store, and suggest improvements to <a href="https://www.twitter.com/designshotsapp">@DesignShotsApp</a></p><p><a href="http://georiot.co/designshots">➤ Download it from App Store!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f4658b706a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>