<?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 Shobhith J B on Medium]]></title>
        <description><![CDATA[Stories by Shobhith J B on Medium]]></description>
        <link>https://medium.com/@shobhith?source=rss-bc890b384ff3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Shobhith J B on Medium</title>
            <link>https://medium.com/@shobhith?source=rss-bc890b384ff3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:49:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shobhith/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[Difference between == and === in Kotlin]]></title>
            <link>https://medium.com/@shobhith/difference-between-and-in-kotlin-9edc281ba27c?source=rss-bc890b384ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/9edc281ba27c</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[equality-operators]]></category>
            <category><![CDATA[referential-equality]]></category>
            <category><![CDATA[structural-equality]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Shobhith J B]]></dc:creator>
            <pubDate>Tue, 13 Jun 2023 13:47:39 GMT</pubDate>
            <atom:updated>2023-08-02T16:01:18.537Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey Guys…!. In this article I am going to explain about <strong>==</strong> and <strong>===</strong> operators in Kotlin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*rV8P8zRaVk9X2sCnL_dl_w.png" /></figure><h4>Introduction</h4><p><strong><em>Equality operators are used to compare the equality of two variables or objects.</em></strong></p><p>If the two passed objects are equal then this will return <strong>true </strong>otherwise will return <strong>false</strong></p><p>Kotlin contains two operators for checking the equality, those are <strong>==</strong> and <strong>===</strong>.</p><p><strong>==</strong> operator in kotlin is called as <strong>Structural Equality Operator.</strong><br><strong>===</strong> operator in kotlin is called as <strong>Referential Equality Operator.</strong></p><h4><strong>What is Structural Equality Operator (==) ?</strong></h4><ul><li>== Operator will check whether the <strong>values/content </strong>of two variables or objects are equal or not.</li><li>This operator is usually used to compare <strong>primitive data types</strong> as this will compare the values of the object/variable passed.</li></ul><h4>What is Referential Equality Operator (===) ?</h4><ul><li>=== Operator will check whether the two variables or objects <strong>refer/points to the same memory location</strong>.</li><li>In simple terms, Both of the variable must point to the same object.</li><li><strong>If we use === with primitive types (such as Int, Double) then the === will be converted to == i.e., it will compare the contents instead of checking for same reference.</strong></li></ul><p><strong>Not understood Properly…!</strong> Don’t worry I will try to simplify the concepts with examples.</p><h4>Examples…!</h4><p>1. First let’s compare two <em>primitive type variable</em>(Int in this case) by using both <strong>==</strong> and <strong>===</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0355d1655785ca57690a01b515d065d7/href">https://medium.com/media/0355d1655785ca57690a01b515d065d7/href</a></iframe><p>Both of the operators will return true. Because,</p><p><em>== operator will compare the values/content of the variable. So, 4==4 is true.</em></p><p><em>In case of === operator, when using === with primitive types, === will be converted to == (Structural Equality Operator</em><strong>) </strong><em>operator. Thus, 4===4 will be converted to 4==4 which is true.</em></p><p>2. Now Let’s create our own custom <em>Student</em> class</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/390ecb92eeab88f17d7758bbe801214a/href">https://medium.com/media/390ecb92eeab88f17d7758bbe801214a/href</a></iframe><h4><strong>Both s1 and s2 has same name(Ramesh), Then How both of the equality checks are false ? 😲</strong></h4><blockquote>Before that you need to know what actually s1 and s2 contains. For that, let’s do <em>println </em>on both s1 and s2.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3611e69fd3327adfbec91447001c8216/href">https://medium.com/media/3611e69fd3327adfbec91447001c8216/href</a></iframe><blockquote>Here you can see both of the objects i.e., s1 and s2 has different values which means that both of them are pointing to different objects. If s1 and s2 was pointing to same location then both would have printed the same value.</blockquote><p>Now, if we look at the previous example;</p><p>Remember the definition of == that it will compare the contents/values. But, I<strong>n case of custom classes == will be converted to ===</strong>.</p><p>So for s1 === s2; both of them are different objects. Because both of them <em>refer/point to different memory locations.</em></p><p>Thus, == and === also results in false.</p><h4><strong>What if we want to compare the values of two classes then? Data classes comes to rescue</strong>🙂</h4><p>If we want to compare the values of two classes then we can use <em>data class</em> instead of normal class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d73cb5236208ca9b5ad478fc7a65e1d/href">https://medium.com/media/7d73cb5236208ca9b5ad478fc7a65e1d/href</a></iframe><p>I think now it makes more sense. If we use data class it will compare the contents of class.</p><h4><strong>In the above examples === was always false. Show me a example where === is true </strong>😑</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d4ed30bccb87d9212a83ea331d8a6cb/href">https://medium.com/media/9d4ed30bccb87d9212a83ea331d8a6cb/href</a></iframe><p>Here s1 and s2 points to same object in the memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*sGiPgQmL8Ya77tlX1dTkzA.png" /></figure><p>If you want to learn more about how objects are created in heap memory and how reference variable works, read the below article.</p><p><a href="https://www.geeksforgeeks.org/reference-variable-in-java/">https://www.geeksforgeeks.org/reference-variable-in-java/</a></p><p>Thanks for reading this article. Happy Learning 🤗</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_x27MIaji8eXxC-r" /><figcaption>Photo by <a href="https://unsplash.com/@whitfieldjordan?utm_source=medium&amp;utm_medium=referral">Jordan Whitfield</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9edc281ba27c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Static Shortcuts in Android]]></title>
            <link>https://medium.com/@shobhith/static-shortcuts-in-android-dc833e1a50d9?source=rss-bc890b384ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc833e1a50d9</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-shortcut]]></category>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Shobhith J B]]></dc:creator>
            <pubDate>Fri, 09 Jun 2023 05:47:04 GMT</pubDate>
            <atom:updated>2023-06-23T04:26:23.860Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey People. In this article I am going to describe about static shortcuts and steps to create them.</p><p>Let’s goo………….!</p><p>If you want to know the basics of shortcuts read this article,</p><p><a href="https://medium.com/@shobhith/shortcuts-in-android-28522cdc93bd">Shortcuts in Android</a></p><p>Static shortcuts are consistent shortcuts which are created and published once, when the launcher activity is opened. Static shortcuts are defined inside a resource file (xml) file.</p><p>These are called static shortcuts because <strong><em>once the static shortcuts are created, it cannot be changed over the lifetime of your app’s current version</em></strong>.</p><p>In simple words, Static shortcuts can only be changed/modified when the new version of the app is updated to the play-store. Because static shortcuts are defined inside xml file, and to change/modify shortcuts as a developer we need to change that particular xml file. And users can see the changes only after we update the app to newer version.</p><p>Good Examples of static shortcuts include Viewing sent messages in Messaging app, Setting an alarm in Clock App, Take Photo in Camera App.</p><h4>How to Create Static Shortcuts ? 🤔</h4><p>There are few steps that you need to follow to create static shortcuts.</p><p><strong>Step1:</strong></p><p>You need to find launcher activity whose intent filters are set to <em>android.intent.action.MAIN </em>action and the <em>android.intent.category.LAUNCHER</em> category inside app’s manifest file (<em>AndroidManifest.xml</em>).</p><p><strong>Step2:</strong></p><p>Add <em>&lt;meta-data&gt;</em> element to launcher activity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52002e52b98defaf603f2f6a992e175f/href">https://medium.com/media/52002e52b98defaf603f2f6a992e175f/href</a></iframe><p>You will get error at this line <em>android:resource=@xml/shortcuts</em>. Don’t worry we will fix this in the next step.</p><p>Here(<em>android:resource=@xml/shortcuts</em>) the filename <em>shortcuts</em> can be anything but it has to match with the name you are going to give in the next step.</p><p><strong>Step3:</strong></p><p>Create a new resource file <em>res/xml/shortcuts.xml.</em></p><p>If you don’t know how to create file. On Android View go to <em>res/xml</em> folder, right click on it. Select <em>New -&gt; Xml Resource File</em>. Give filename as <em>shortcuts</em> and press OK.</p><p><strong>Step 4:</strong></p><p>In the newly created shortcuts.xml, add a <em>&lt;shortcuts&gt;</em> root element, which contains multiple <em>&lt;shortcut&gt;</em> elements. Each <em>&lt;shortcut&gt; </em>element is used to create a new shortcut which contains information like its icon, its description labels, and the intents that it launches when we click on particular shortcut.</p><p>Here I will create shortcut for writing new messages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40c6b702f3d2ed0e04ca748376453635/href">https://medium.com/media/40c6b702f3d2ed0e04ca748376453635/href</a></iframe><blockquote>For every shortcut <strong><em>android:shortcutId</em></strong> and <strong><em>android:shortcutShortLabel</em></strong> is mandatory and rest of the properties are optional.</blockquote><p>First Let’s look at the output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4AjJy4UbFzFqtU8OeiODPw.png" /></figure><p>Now Let’s see each of the attribute in detail:</p><p><strong>android: shortcutId</strong>: A string literal, which is used to identify particular shortcut. And you can’t set this attribute’s value to a resource string, such as <em>@string/id</em>.</p><p><strong>android:shortcutShortLabel</strong>: A short text which describes what the shortcut does. This attribute’s value must be a resource string, such as <em>@string/short_label.</em></p><p><strong>android:shortcutLongLabel</strong>: A long text which describes what the shortcut does. If there’s enough space, the launcher displays this value instead of <em>android:shortcutShortLabel. </em>This attribute’s value must be a resource string, such as <em>@string/long_label</em>. This is <em>optional.</em></p><p><strong>android:shortcutDisabledMessage</strong>: The message which appears when user attempts to launch a disabled shortcut. Static shortcuts can be disabled by setting <em>android:enabled</em> attribute to <em>false. </em>This attribute’s value must be a resource string, such as @string/disabled<em>_label</em>. This is <em>optional.</em></p><p><strong>android:icon</strong>: The icon which is displayed to the user for a particular shortcut when users long click on app. This is <em>optional.</em></p><p><strong>&lt;intent&gt; </strong>:tThe action that the system performs when the user clicks on the shortcut. The intent includes android:action, android:targetPackage, android:targetClass. This is <em>optional.</em></p><p><strong>&lt;categories&gt;</strong> : Provides a grouping for the types of actions that your app’s shortcuts perform. This is <em>optional.</em></p><p><strong>&lt;capability-binding&gt;</strong> : Capability binding enables users to use spoken commands with Google Assistant to invoke this shortcut whenever we use built in intents. This is <em>optional.</em></p><p>Yup….That’s it for this article. Thank you for reading. Until Next Time. Happy Coding 😉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8gomFvWXOfL9RI5b" /><figcaption>Photo by <a href="https://unsplash.com/@fossy?utm_source=medium&amp;utm_medium=referral">Fab Lentz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc833e1a50d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Shortcuts in Android]]></title>
            <link>https://medium.com/@shobhith/shortcuts-in-android-28522cdc93bd?source=rss-bc890b384ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/28522cdc93bd</guid>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-shortcut]]></category>
            <category><![CDATA[android-trick]]></category>
            <dc:creator><![CDATA[Shobhith J B]]></dc:creator>
            <pubDate>Sun, 03 Apr 2022 16:36:11 GMT</pubDate>
            <atom:updated>2023-06-09T06:09:01.123Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*xnYvTBTmJiGIVEo74t9Trg.png" /></figure><p>Hello contrivers, In this article I am going to give a quick introduction of Android shortcuts. As this is just an introduction article, I am not deep diving into Types of shortcuts here.</p><p>Vroom Vroom….Let’s gooo………</p><h3>Shortcuts in Android…..</h3><p>In general, shortcut is an alternative route that is shorter than the one usually taken or means of doing something more directly and quickly than ordinary procedure.</p><p>Android shortcuts are also created with the same intention. i.e., to ease the procedure of performing any action.</p><p><strong><em>Android introduced shortcuts to perform some actions on our apps.</em></strong></p><p>As the <em>shortcuts</em> are displayed in a <em>supported launcher or assistant</em>, like Google Assistant, it will help users to <em>quickly and easily</em> perform common tasks within our app without navigating to the screen where it contains the actual action to be performed.</p><p><strong><em>Still haven’t understood? Let me give you an example……</em></strong></p><p>Choose any app like whatsapp, instagram or camera app. Long press on the app icon. Some apps will display a popup dialog. The popup dialog contains options such as <em>Camera, Contact Names</em> in case of <em>Whatsapp</em>; <em>Camera,New Post,View activity, Chats </em>in case of <em>Instagram</em>; <em>Video, Photo</em> in case of <em>Camera</em> <em>App</em>. <strong><em>The options in these popups are called as shortcuts.</em></strong></p><blockquote>These options in the popup may vary for different apps and for different users.</blockquote><p>Let’s see some of the actions that we can perform with shortcuts,</p><h3>Shortcut Examples</h3><p>➼ Composing new email in an email app.</p><p>➼ Sending message to a friend in a messaging app.</p><p>➼ Ordering food in delivery app using voice command.</p><p>➼ Adding a new post in a social media app.</p><p>➼ Opening new tab in web browser app.</p><p>➼ Sending money in Banking app.</p><blockquote>All these examples and many more actions can be performed from launcher icon or using assistants directly and quickly without actually navigating to the screen where it has these actions.</blockquote><h3>Types of Shortcuts</h3><p>Shortcuts are categorized into three types. They are,</p><h4>Static Shortcuts</h4><p>These type of shortcuts are defined in a resource file (xml file) that is packaged into an APK. These shortcuts are created and published once when the launcher activity is opened.</p><p><em>To learn how to create Static shortcut read the below docs,</em></p><p><a href="https://medium.com/@shobhith/static-shortcuts-in-android-dc833e1a50d9">Static Shortcuts in Android</a></p><h4>Dynamic Shortcuts</h4><p>These type of shortcuts are created,pushed,updated and removed by your app only at runtime using <em>ShortcutManager API</em>.</p><p><em>To learn how to create Dynamic shortcut read the below docs,</em></p><p><a href="https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts#dynamic">https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts#dynamic</a></p><h4>Pinned Shortcuts</h4><p>Pinned Shortcuts are supported only in android version 8.0 and above. Unlike static or dynamic shortcuts pinned shortcuts appear as a separate icon in supported launchers at runtime only if the user grants the permission to do so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*KwE7UCe1f1FQl88caZDrYA.png" /></figure><p><em>To learn how to create Pinned shortcut read the below docs,</em></p><p><a href="https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts#pinned">https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts#pinned</a></p><p>Thank you for reading this article. Happy Coding 😊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GdPBkPFPo8CuwkEy" /><figcaption>Photo by <a href="https://unsplash.com/@randytarampi?utm_source=medium&amp;utm_medium=referral">Randy Tarampi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28522cdc93bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[View Binding in Android : Activity, Fragment]]></title>
            <link>https://medium.com/@shobhith/view-binding-in-android-activity-fragment-3d8ed995e276?source=rss-bc890b384ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d8ed995e276</guid>
            <category><![CDATA[findviewbyid]]></category>
            <category><![CDATA[view-binding]]></category>
            <category><![CDATA[android-view-binding]]></category>
            <category><![CDATA[android-activity]]></category>
            <category><![CDATA[android-views]]></category>
            <dc:creator><![CDATA[Shobhith J B]]></dc:creator>
            <pubDate>Thu, 07 Oct 2021 14:22:53 GMT</pubDate>
            <atom:updated>2021-11-02T10:37:00.726Z</atom:updated>
            <content:encoded><![CDATA[<h3>View Binding in Android : Activity, Fragment</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R2hsdZP-GkhnuvHnxp0lwQ.png" /></figure><h4>It’s time to say good bye to <a href="https://developer.android.com/reference/android/view/View#findViewById(int)"><strong><em>findViewById</em></strong></a></h4><p>Yes, you heard it right!. With the introduction of a new feature called <strong><em>ViewBinding </em></strong>which is a part of<strong><em> jetpack, </em></strong>now developers do not<strong><em> </em></strong>need to use <strong><em>findViewById</em></strong> to access views from xml layout file<em>.</em></p><h3>Disadvantages of findViewById 😐</h3><p><strong>Not Null Safe</strong> : findViewById can create NullPointer Exception. findViewById creates a possibility of accessing invalid ID which is present in any other layout file. i.e, we can access ID of any view from any layout file which sometimes will result in null,because the java or kotlin file can only access views associated with it’s layout (layout set using setContentView) not from other layouts.</p><p><strong>Not Type Safe</strong> : The return type of findViewById is generic view, that means if we need a reference of EditText or TextView then we have to manually cast it to the required type. Assume I have a button in layout.xml file and I need a reference of that button in activity.java or activity.kt file. If I accidentally cast it to TextView, findViewById won’t complaint about it but the App will crash at Runtime.</p><p><strong>Tedious</strong> : Using findViewById can be tedious. Just to initialize views We have to write many lines of code which makes the code less readable,less maintainable and more complex.</p><p>Here comes ViewBinding to rescue . シ</p><h3><strong>What is ViewBinding ?</strong></h3><p>According to the official documentation ViewBinding is stated as,</p><p>＂<em>View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.</em>＂</p><p>In Simple terms, View Binding is an android feature which is used to access views such as <em>TextView, Button</em> etc., directly. You can think view binding as an alternative to findViewById. View Binding does the same job as findViewById with added advantages like null safety, type safety and ease of view access.</p><h3>Let’s Setup ViewBinding…</h3><p>To enable ViewBinding, set the ViewBinding build option to <em>true</em> in the module level <em>build.gradle</em> file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe5d0f06412b1d0f3167b410b31029ba/href">https://medium.com/media/fe5d0f06412b1d0f3167b410b31029ba/href</a></iframe><p><em>That’s it ! Only two lines and we are done with setting up ViewBinding.</em></p><h3><strong>Use ViewBinding…</strong></h3><p>When we enable <em>ViewBinding</em> in module level <em>build.gradle, a binding class </em>is generated for each of the layout file. The each binding class generated contains reference to <em>all the views which has an ID</em> and also contains reference to <em>root view</em>.</p><p>The name of the binding class is given by converting the layout file name to Pascal Case and adding “<em>Binding” </em>to the end.</p><p>Let’s take the below example file named <em>activity_login.xml</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d46cccaaa7a799099c56c1f960ba6b1a/href">https://medium.com/media/d46cccaaa7a799099c56c1f960ba6b1a/href</a></iframe><p>The generated binding class will have the name <strong><em>ActivityLoginBinding. </em></strong>And it will contain reference to views such as <em>Button</em>, two <em>EditText’s</em> but it doesn’t contain reference to <em>TextView</em> because the <em>TextView</em> has no ID.</p><p>Similarly, the generated binding class i.e, <strong><em>ActivityLoginBinding </em></strong>will contain reference to <em>LinearLayout</em> <em>root view</em>. The root view reference can be accessed using<em> </em><strong><em>getRoot()</em></strong><em> </em>method.</p><p>Till Now we have discussed how view binding works.! Next let’s discuss how to reference views by creating instance of binding class from activity and fragment.</p><h4>ViewBinding in Activity</h4><p>To access or reference views within activity we have to first setup an instance of binding class inside <strong><em>onCreate()</em></strong> . To create an instance follow the below steps,</p><p>☛ Call the <strong><em>inflate() </em></strong>method which is present in generated binding class. This will give us the instance of binding class. <strong><em>inflate()</em></strong> is a <em>static method</em>, so you can directly call that with the help of binding class name and dot operator.</p><p>☛ Get access to the root view by calling <strong><em>getRoot() </em></strong>method<strong><em> </em></strong>using<strong><em> </em></strong>the binding instance<em>.</em></p><p>☛ Pass the root view to the <strong><em>setContentView() </em></strong>method of activity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a975ff86ff9242f4718ad17dcb05dff/href">https://medium.com/media/4a975ff86ff9242f4718ad17dcb05dff/href</a></iframe><p>Now you can use binding class instance (<em>loginBinding</em>) to access the views.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c006944a71f6b6fd0ad7ae0b1e56d63/href">https://medium.com/media/3c006944a71f6b6fd0ad7ae0b1e56d63/href</a></iframe><p>The generated binding class converts view ID into camelCase and provide direct access to views using ID.</p><h4>ViewBinding in Fragments</h4><p>To access or reference views within fragment we have to first setup an instance of binding class inside <strong><em>onCreateView()</em></strong> . To create an instance follow the below steps,</p><p>☛ Call the <strong><em>inflate() </em></strong>method which is present in generated binding class. This will give us the instance of binding class. <strong><em>inflate()</em></strong> is a <em>static method</em>, so you can directly call that with the help of binding class name and dot operator.</p><p>☛ Get access to the root view by calling <strong><em>getRoot() </em></strong>method<strong><em> </em></strong>using<strong><em> </em></strong>the binding instance<em>.</em></p><p>☛ return the root view from the <strong><em>onCreateView() </em></strong>method of fragment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba4bf1d25057bc9a664a1e0f95c1f45b/href">https://medium.com/media/ba4bf1d25057bc9a664a1e0f95c1f45b/href</a></iframe><p>If the layout is already inflated then you can use <strong><em>bind()</em></strong> method of binding class inside <strong><em>onViewCreated()</em></strong> of fragment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc527d095863ca8916ee3d9aa605c759/href">https://medium.com/media/fc527d095863ca8916ee3d9aa605c759/href</a></iframe><p>Here in both cases <em>_homeBinding</em> will be only valid between <em>onCreateView()</em> and <em>onDestroyView().</em></p><p>Now you can use binding class instance (home<em>Binding</em>) to access the views.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1d4e99c833cd115f154ce2efae42459/href">https://medium.com/media/d1d4e99c833cd115f154ce2efae42459/href</a></iframe><h3><strong>Ignore a Layout …</strong>✗</h3><p>When you enable <em>viewBinding</em> in module level <em>build.gradle,a binding class will be generated for each and every layout which is in the module.</em></p><p><strong><em>But what if you don’t want to generate binding class for a layout ? is it possible?</em></strong></p><p>Yes…We can completely ignore a layout from generating binding class. How ?</p><p>Set the attribute <strong><em>tools:viewBindingIgnore = ”true”</em></strong> to the root view of the layout.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aed61cf6197282f07f320cead7e8042/href">https://medium.com/media/2aed61cf6197282f07f320cead7e8042/href</a></iframe><h3>Advantages of View Binding…😉</h3><p><strong>Null Safe</strong> : ViewBinding is null safe, as it will provide access to the views which are present only in the corresponding layout. i.e, java or kotlin file can only get reference of views associated with it’s layout not from other layouts.</p><p><strong>Type Safe</strong> : The return type of findViewById is generic view, that means if we need a reference of EditText or TextView then we have to manually cast it to the required type. But View Binding returns exact type of view, even if it’s a custom view.</p><p><strong>Speed : </strong>Even though view binding generates binding class it will not make any impact on the build speed.</p><p><strong>Dynamic : </strong>View Binding is dynamic i.e, If we add any new view elements in the XML, we will get reference to that view element immediately without rebuilding.</p><h3>Disadvantage of View Binding…😱</h3><p>View Binding actually has <em>no disadvantages</em> but personally i found one disadvantage,</p><p><strong>More Overhead :</strong></p><p>View Binding generates binding class for each and every layout, so it may increase the size of project and build time a little bit. But these changes will be very small.</p><p>Now you will say that we can set <strong><em>tools:viewBindingIgnore = ”true” </em></strong>attribute,Yes it’s possible. But what if the project is huge with large number of XML files. It will be an overhead for the developer to set this attribute in many layouts<strong><em>.</em></strong></p><p>See the full code here ☟</p><p><a href="https://github.com/Shobhith30/View-Binding">GitHub - Shobhith30/View-Binding: View Binding with sample example</a></p><p><strong><em>Good bye findViewById()</em></strong>……</p><p>That’s it from my side….. If you want to learn more about view binding visit the ☟ link</p><p><a href="https://developer.android.com/topic/libraries/view-binding">https://developer.android.com/topic/libraries/view-binding</a></p><p>Thank you for reading ! Happy coding ! 😎</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sh5n-Mfse-d7iRLN" /><figcaption>Photo by <a href="https://unsplash.com/@theblowup?utm_source=medium&amp;utm_medium=referral">the blowup</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d8ed995e276" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Activity State Changes]]></title>
            <link>https://medium.com/@shobhith/activity-state-changes-a6f3c82e16f7?source=rss-bc890b384ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6f3c82e16f7</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[activity-state]]></category>
            <category><![CDATA[background-process]]></category>
            <category><![CDATA[android-activity]]></category>
            <category><![CDATA[android-process]]></category>
            <dc:creator><![CDATA[Shobhith J B]]></dc:creator>
            <pubDate>Mon, 04 Oct 2021 15:06:01 GMT</pubDate>
            <atom:updated>2023-06-23T04:22:19.588Z</atom:updated>
            <content:encoded><![CDATA[<h3>Activity Lifecycle Changes: Events</h3><p>Hello Android Devs, In this article I am going to explain about activity state / lifecycle changes due to different user or system events.</p><p>Lets Go…….!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lWmT_e3sEFCsEqK2F6kXcw.jpeg" /></figure><h4>Introduction</h4><p>Some user triggered or system triggered events like screen rotation, switching to multi window mode etc., can cause activity to change from one state to another. During these events the activity may go through different lifecycle states.</p><p>So let’s discuss some of the events that occur and how it will affect the activity state.</p><h4>1〉 Configuration Change</h4><p>There are number of reasons for configuration change to occur. Some of the common examples are Screen rotation, Changing device language, Switching to multi window mode etc,.</p><p>What happens to activity when configuration change occurs?. The activity will be destroyed and a new instance of same activity will be created.</p><p>During configuration change all the views which has an ID will retain it’s state internally and views in new instance of the activity contains the previous view state. For example when you enter some text in edittext which has an ID and rotate the screen the text entered in the edittext will be retained.</p><p>The activity will go through the following lifecycle during configuration change</p><p><strong><em>onPause() </em></strong>➟ <strong><em>onStop() </em></strong>➟ <strong><em>onSaveInstanceState() </em></strong>➟ <strong><em>onDestroy()</em></strong> on Original activity instance</p><p><strong><em>onCreate() </em></strong>➟ <strong><em>onStart() </em></strong>➟ <strong><em>onRestoreInstanceState() </em></strong>➟ <strong><em>onResume()</em></strong> on new activity instance.</p><blockquote>All the view which has an ID will preserve it’s state during configuration change but if you want to save additional data, use <strong><em>onSaveInstanceState, ViewModels</em></strong> and <strong><em>persistent local storage</em></strong> like <strong><em>shared preferences, Room DB</em></strong> etc., to preserve the data. As a developer, you can any of these or combination of these options to preserve data according to your need.</blockquote><h4>2〉 User Taps Back Button</h4><p>If the user is currently interacting with the activity and presses the back button then, the activity will be destroyed permanently by the android system.</p><p>In this case saving activity state is not required because <strong><em>back button</em></strong> press is considered as normal behavior i.e, user permanently want to destroy the current instance of activity because he don’t want to return to the same instance of the activity.</p><p>The activity will go through the following lifecycle during back button press</p><p><strong><em>onPause() </em></strong>➟ <strong><em>onStop() </em></strong>➟ <strong><em>onDestroy()</em></strong></p><blockquote><strong><em>onSaveInstanceState() </em></strong><em>is not called because user don’t want to return to the same instance of the activity, so no need to save activity state.</em></blockquote><p>When user taps on back button, it will trigger one additional callback method , <strong><em>onBackPressed(). </em></strong>Here you can write code to perform any logic, when back button is pressed.</p><h4>3〉 Activity or dialog appears in foreground</h4><p>As the name says, we are going to discuss what happens when an activity or dialog appears on top of the current activity. We can divide this case into two scenarios,</p><p><strong><em>Scenario 1 : Activity or dialog partially covers current activity</em></strong></p><p>If the user interacting with the current activity and opens any dialog or activity which covers the current activity partially then, the current activity will lose focus and new activity or dialog will gain focus. i.e, the current activity will be still visible partially to the user but the user can’t interact with it. In this case the current activity will go though below lifecycle callbacks,</p><p><strong><em>onPause()</em></strong></p><p>Whenever new activity or dialog is closed, current activity will gain focus and come to foreground. In this case the current activity will go though below lifecycle callbacks,</p><p><strong><em>onResume()</em></strong></p><p><strong><em>Scenario 2: Activity or dialog completely covers current activity</em></strong></p><p>If the user interacting with the current activity and opens any dialog or activity which covers the current activity completely then, the current activity will lose focus and new activity or dialog will gain focus. i.e, the current activity will not be visible to the user at all. In this case the current activity will go though below lifecycle callbacks,</p><p><strong><em>onPause() </em></strong><em>➟ </em><strong><em>onStop()</em></strong></p><p>Whenever new activity or dialog is closed, current activity will gain focus and come to foreground. In this case the current activity will go though below lifecycle callbacks,</p><p><strong><em>onRestart() </em></strong><em>➟ </em><strong><em>onStart() </em></strong><em>➟ </em><strong><em>onResume()</em></strong></p><blockquote>When user taps on<strong> Home Button</strong> in mobile phone, the activity will fall under scenario 2 .i.e, Current activity behaves exactly like ”<strong><em>Scenario 2: Activity or dialog completely covers current activity</em></strong> ”.</blockquote><h4>4〉 System kills app process</h4><p>The system kills an app process to free up the memory and allocate this free memory to other app process which has higher priority than currently running app.</p><p>You may have noticed that, you opened instagram and after using it for few minutes you pressed home button and then opened whatsapp. After spending few minutes in whatsapp , if you again open instagram from recents screen then sometimes it will open at the exact last screen or last post that you have visited but sometimes it will open from beginning, as your opening a closed app.</p><p>This is because the instagram app process will be kept when there is enough memory for whatsapp to run smoothly. If there is additional memory requirement then instagram process will be killed.</p><p>The priority of killing app process is determined by the android system based on the state of that process. The priority of the process in turn depends on the priority of the activity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*UixmSdSoYS3JUfoYr0OB7Q.png" /><figcaption>source <a href="https://developer.android.com/guide/components/activities/activity-lifecycle">developer.android.com</a></figcaption></figure><p>After deciding which activity or process has less priority, the android system will kill the entire app process instead of just killing the activity. So, every components like activity, service, receivers will be killed which were running in the app process.</p><p>For more information visit the below links,</p><p><a href="https://developer.android.com/guide/components/activities/state-changes">https://developer.android.com/guide/components/activities/state-changes</a></p><p><a href="https://developer.android.com/guide/components/processes-and-threads">https://developer.android.com/guide/components/processes-and-threads</a></p><p><a href="https://developer.android.com/guide/components/activities/activity-lifecycle">https://developer.android.com/guide/components/activities/activity-lifecycle</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xHS-hdIISGmYTl-V" /><figcaption>Photo by <a href="https://unsplash.com/@helloimnik?utm_source=medium&amp;utm_medium=referral">Hello I&#39;m Nik</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>That’s it from my side. Thank you for reading this article! Happy learning 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6f3c82e16f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OnSaveInstanceState and OnRestoreInstanceState in Android]]></title>
            <link>https://medium.com/@shobhith/onsaveinstancestate-and-onrestoreinstancestate-in-android-c29f22b0ef4e?source=rss-bc890b384ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c29f22b0ef4e</guid>
            <category><![CDATA[onsaveinstancestate]]></category>
            <category><![CDATA[ui-state]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[activity-state]]></category>
            <category><![CDATA[android-activity]]></category>
            <dc:creator><![CDATA[Shobhith J B]]></dc:creator>
            <pubDate>Thu, 30 Sep 2021 04:34:31 GMT</pubDate>
            <atom:updated>2023-05-12T15:38:12.890Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi Android Devs, Today I am going to explain about saving and restoring activity UI state using two important techniques; <strong>onSaveInstanceState</strong> and <strong>onRestoreInstanceState.</strong></p><h3>Before we start :</h3><p>In this entire article, I am going to take the example of counter app which will increment / decrement the counter value on button click.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*FdGT8-i4YvZfLFgYlshm4w.png" /><figcaption>Counter App</figcaption></figure><h3>First of all what is Instance State?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BQ6hYn8r3Ey9Cxl0" /><figcaption>Photo by <a href="https://unsplash.com/@jannerboy62?utm_source=medium&amp;utm_medium=referral">Nick Fewings</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The term <strong>Instance State</strong> sounds difficult. But don’t worry, I will explain it in detail.</p><p>The normal activity behavior is that; <em>activity will be destroyed permanently</em> when the user taps on the <strong>back button</strong> <em>OR </em>activity finishes itself <em>by calling it’s </em><strong><em>finish() </em></strong><em>method</em>.</p><p>So, Next time when the same activity is opened again then a whole new fresh activity will be created (i.e., New Instance of Activity).</p><p>The life cycle callback of activity for the above behavior will be:</p><blockquote><strong>onPause() </strong>➟ <strong>onStop() </strong>➟ <strong>onDestroy() </strong>➟ Same Activity Opened Again<strong> </strong>➟ <strong>onCreate() </strong>➟ <strong>onStart() </strong>➟ <strong>onResume()</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYloipkuKWmF3HvcCj9kZQ.gif" /><figcaption>Normal App behaviour</figcaption></figure><p>The above example shows normal app behavior. Whenever user taps on back button intentionally, the activity will be destroyed permanently.</p><p>If the same activity is opened again then the <em>counter value</em> (8) and <em>name</em> in the E<em>ditText</em> is cleared. This is expected because back button on activity means that the user want to leave that activity, So no need to remember anything which was there previously (counter value and name in <em>EditText</em>).</p><p>But…………?</p><h4>But What?</h4><p>What happens if the activity gets destroyed as a result of <strong>configuration change or process death</strong>. It’s not an expected behavior. So, the user may want to return to the same activity sooner and might want the activity to have the same state (data entered in <em>EditText</em>, previous counter value ) as it was there before.</p><p>To achieve this, android system will remember that the activity had existed before. So, when same activity is opened again ,then a fresh new activity will be created but with the <strong><em>remembered data / saved state</em></strong> ( previous counter value , name in the <em>EditText</em> before).</p><p><strong>“The saved data, that is used to restore or retain the previous state of activity is called as instance state</strong>.”</p><p>In simple words we can say that, The counter value and name in the <em>EditText </em>are kind of Instance States. Because, they are used to restore the previous state of that activity.</p><p>The life cycle callback for the above behavior will be:</p><blockquote><strong>onPause() </strong>➟ <strong>onStop() </strong>➟ <strong>onSaveInstanceState() </strong>➟ <strong>onDestroy() </strong>➟ Same Activity Opened Again<strong> </strong>➟ <strong>onCreate() </strong>➟ <strong>onStart() </strong>➟ <strong>onRestoreInstanceState() </strong>➟ <strong>onResume()</strong></blockquote><p>Note:- On Pre Pie Devices <em>OnSaveInstanceState()</em> is called before <em>onStop().</em></p><p>You can see two additional lifecycle callback methods above; <strong>onSaveInstanceState() </strong>and<strong> onRestoreInstanceState()</strong></p><p>Let’s discuss them in the next section!</p><h3>Why onSaveInstanceState() ?</h3><p>When Activity undergoes configuration change or process death it is important to save the state of activity, so that when the same activity is opened in future, user want to see that activity with the same previous state.</p><p>By default the<strong> state of the view </strong>( data in EditText, ListView scoll position ) is saved by the android system if the view has an ID. In this case <strong>onSaveInstanceState()</strong> is called internally by the android system.</p><p>But, what if the user wants to save additional information (like counter value) that has to be the preserved even if the activity undergoes configuration change then developers has to override,</p><pre><em>onSaveInstanceState(outState: Bundle?)</em></pre><p>Here, one can save simple primitive data in key-value pairs using <strong>Bundle</strong> object provided.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f96ab6dffe3252d84f62416dccc0ec8/href">https://medium.com/media/2f96ab6dffe3252d84f62416dccc0ec8/href</a></iframe><blockquote>SCORE_KEY -&gt; string key used to save counter value in bundle</blockquote><blockquote>countValue -&gt; integer value which contains actual value of counter</blockquote><h3>Then What is onRestoreInstanceState() ?</h3><p>In simple terms, this method is used to restore state which is saved using onSaveInstanceState().</p><p>Remember that, this callback will be called only if the activity is re opened after it had undergone configuration change or process death.</p><p>The view state which was saved automatically will be restored automatically by the android system by calling <strong>onRestoreInstanceState()</strong> internally.</p><p>To restore the data saved by you , then you have to override,</p><pre><em>onRestoreInstanceState(savedState: Bundle?)</em></pre><p>Here we can make use of Bundle to retrieve saved data using keys.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4aad97d11dc16f7dc092998d1e307c91/href">https://medium.com/media/4aad97d11dc16f7dc092998d1e307c91/href</a></iframe><blockquote><em>SCORE_KEY </em>-&gt; string key used to retrive saved counter value in bundle</blockquote><blockquote><em>countValue</em> -&gt; integer variable which holds the counter value throughout the activity</blockquote><h3>Do we really need onRestoreInstanceState()</h3><p>The answer is <strong>NO, </strong>Whaaaaaaaaat?</p><p>Yes we can even restore the saved state in <strong>onCreate().</strong></p><p>Remember the syntax of onCreate,</p><pre>onCreate(savedInstanceState: Bundle?)</pre><p>Here the <em>savedInstanceState</em> contains previous state saved using <strong>onSaveInstanceState()</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4690988ef398b45ba9649958a074ea6/href">https://medium.com/media/e4690988ef398b45ba9649958a074ea6/href</a></iframe><blockquote>if there are no previously saved states then the Bundle will be <strong>null; </strong>therefore we have to check whether<strong> </strong><em>savedInstanceState</em> is null before retrieving the saved value.</blockquote><h3>Let’s see the Result :</h3><p><strong><em>Without saving Activity State (onSave and onRestore Instance State)</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oPX3epf4r2Y9vuvNcFAgrQ.gif" /><figcaption>Without Saving State</figcaption></figure><p>Here in the above example, we are not using <strong>OnSaveInstanceState() and OnRestoreInstanceState()</strong>. You can see from the above video that whenever <strong>configuration change</strong> occurs (screen rotation) the counter value is not preserved / saved and therefore counter value = 0 after every screen rotation, because it is the default value we are assigning initially.</p><p>But the name in the <em>EditText </em>is not cleared ,why? because the system internally calls <strong>OnSaveInstanceState() </strong>and <strong>OnRestoreInstanceState()</strong> on Views which has an ID.</p><blockquote><strong>Exercise for you:<em> </em></strong><em>I will give you a small task. Create a simple </em>EditText <em>and don’t give any </em>ID <em>attribute to it. Try to rotate your device to see what happens?, do the same by giving ID to </em>EditText<em>. <br>Comment Down your Findings 😜</em></blockquote><p><strong><em>With saving Activity State (onSave and onRestore Instance State)</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bglt5rZ59B7VoaU3Qh5_qg.gif" /><figcaption>With Saving State</figcaption></figure><p>Here, we are using <strong>OnSaveInstanceState() and OnRestoreInstanceState()</strong>. You can see from the above video that whenever <strong>configuration change</strong> occurs (screen rotation) the counter value is preserved / saved and therefore counter value is same as it was before rotating the screen.</p><p>You can find full code in my github repository linked below;</p><p><a href="https://github.com/Shobhith30/Activity-Instance-State">GitHub - Shobhith30/Activity-Instance-State</a></p><p>If you want to learn more about saving activity state check the official documentation</p><p><a href="https://developer.android.com/guide/components/activities/activity-lifecycle.html?hl=sk#saras">https://developer.android.com/guide/components/activities/activity-lifecycle.html</a></p><p>Yaaaay, that’s it about saving states in activity. This is my first post on medium. So, pardon me if there are any mistakes.</p><p>Thank you for reading! Happy Coding ! 😊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LkjYXQzPmq3NpFm6" /><figcaption>Photo by <a href="https://unsplash.com/@olenkasergienko?utm_source=medium&amp;utm_medium=referral">Olena Sergienko</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c29f22b0ef4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>