<?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 Ibrahim Yilmaz on Medium]]></title>
        <description><![CDATA[Stories by Ibrahim Yilmaz on Medium]]></description>
        <link>https://medium.com/@cs-ibrahimyilmaz?source=rss-bfa5f552d0f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9QGrtxc07elpHLdw3Nis6A.jpeg</url>
            <title>Stories by Ibrahim Yilmaz on Medium</title>
            <link>https://medium.com/@cs-ibrahimyilmaz?source=rss-bfa5f552d0f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 03:10:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cs-ibrahimyilmaz/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[Build Better and Clean RecyclerView.Adapter With Kiel]]></title>
            <link>https://medium.com/swlh/build-better-and-clean-recyclerview-adapter-with-kiel-a129882c1e1?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a129882c1e1</guid>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Wed, 12 Aug 2020 10:11:49 GMT</pubDate>
            <atom:updated>2020-08-13T19:09:16.973Z</atom:updated>
            <content:encoded><![CDATA[<h4>Duplicate code is the root of all evil in software design. When a system is littered with many snippets of indentical, or nearly identical code, it is indicative of sloppiness, carelessness, and sheer unprofessionalism. It is the guilt-edged responsibility of all software developers to root out and eliminate duplication whenever they find it.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ow6Iqnt6vnd56gcY" /><figcaption>Photo by <a href="https://unsplash.com/@bailey_i?utm_source=medium&amp;utm_medium=referral">Issy Bailey</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Some years ago, we had only ListView and its Adapter. It had performance issues and these issues were resolved with ViewHolder Pattern (Here also I want to send my greatest appreciation to the one who found and shared this).</p><p>If we go back and recall it in detail, it is basically wrapping some view related software entities into ViewHolder class, saving it in View’s tag and using it in. Hopefully, Google released better ListView, which is RecyclerView, for us.</p><p>As we are in the mud of Android Hell (lifecycle problems, architecture problems, UI development problems etc etc.), most of us like did not notice the adapter hell in ListView. We did not complain about it enough as we did in this ViewHolder pattern, so Google has not saved us from this hell yet.</p><h4>What is Adapter Hell?</h4><p>Writing/Duplicating the same RecyclerView.Adapter implementation for each Fragment/Activity which uses RecyclerView to visualize the data. Code Duplication is bad but duplicating bug or limitation is worser thing that we can do for us🤢🤢🤢.</p><p>In Twitter, there was a confession post-cycle for Android Developers and Duplicating RecyclerView.Adapter is the one of the most popular answers by Android Developers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qGXlA_Nxr4Ao5cF0" /><figcaption>Photo by <a href="https://unsplash.com/@tq_photos?utm_source=medium&amp;utm_medium=referral">T. Q.</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If there are more than one type to be visualized in RecyclerView, it means more wood in the fire of this hell 🥵.</p><p>Because it means that you will have:</p><ul><li>more if/when/switch statement 😥</li><li>more casting 🤯</li><li>more bug🤬</li></ul><p>There are many very good solutions and articles about these issues.</p><p>One of the most popular idea is based on Delegation Pattern over RecyclerView.Adapters, which is basically we create a generic adapter, we have delegation for each type which help us to create ViewHolder and binding data to ViewHolder.</p><p>You should absolutely read<a href="https://twitter.com/sockeqwe"> Hannes Dorfmann</a>’s Adapter Hell Escape Article. I want to say thanks to him for such a great article and AdapterDelegates library. It still helps a lot.</p><p><a href="http://hannesdorfmann.com/android/adapter-delegates">Joe&#39;s great adapter hell escape</a></p><p>Another pain point is the viewtypes. It should be unique arbitrary Integer. Our data types in the adapter are unique, so we have different data type for different views. But we have to define this Integer value😢.</p><p>Should we define them as 1,2,3 or 4?</p><p>It makes me remember button1, button2, button3…</p><p>Unfortunately ,we can’t use that data types hash value because HashValue is integer. There can be collision!😳</p><p>Don’t worry there is also a solution for that, which is basically using layout resource as a viewType! It is unique, it is logically connected with both ViewHolder and data type.</p><p>You should absolutely read <a href="https://medium.com/@dpreussler">Danny Preusler</a>’s article about that.</p><p><a href="https://proandroiddev.com/writing-better-adapters-1b09758407d2">Writing Better Adapters</a></p><p>So the idea:</p><ul><li>Don’t copy/paste/write same adapter code for each RecyclerView.</li><li>Use your layout resource as a viewType.</li></ul><h3>RecyclerView.Adapter with Kiel</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pSUXu3pqHJNOuIxq" /><figcaption>Photo by <a href="https://unsplash.com/@jeremythomasphoto?utm_source=medium&amp;utm_medium=referral">Jeremy Thomas</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://github.com/ibrahimyilmaz/kiel">Kiel</a> is designed to be easy to integrate, easy to use based on these two ideas with the help of the power of Kotlin💪.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28255%252C255%252C255%252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520%252520%252520%252520val%252520adapter%252520%25253D%252520adapterOf%25253CType%25253E%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520diff%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520areItemsTheSame%252520%25253D%252520%25257B%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520areContentsTheSame%252520%25253D%252520%25257B%252520%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520getChangePayload%252520%25253D%252520%25257B%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%29%25250A%252520%252520%252520%252520%252520%252520%252520register%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520viewHolder%252520%25253D%252520%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520layoutResource%252520%25253D%252520%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520onViewHolderCreated%252520%25253D%252520%25257B%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520onBindViewHolder%252520%25253D%252520%25257B%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520onBindViewHolderWithPayload%252520%25253D%252520%25257B%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%29%25250A%252520%252520%252520%252520%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28255%25252C255%25252C255%25252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520%25252520%25252520%25252520val%25252520adapter%25252520%2525253D%25252520adapterOf%2525253CType%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520diff%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520areItemsTheSame%25252520%2525253D%25252520%2525257B%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520areContentsTheSame%25252520%2525253D%25252520%2525257B%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520getChangePayload%25252520%2525253D%25252520%2525257B%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520register%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520viewHolder%25252520%2525253D%25252520%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520layoutResource%25252520%2525253D%25252520%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520onViewHolderCreated%25252520%2525253D%25252520%2525257B%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520onBindViewHolder%25252520%2525253D%25252520%2525257B%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520onBindViewHolderWithPayload%25252520%2525253D%25252520%2525257B%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29%2525250A%25252520%25252520%25252520%25252520%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fee7a5ad33c3b0915fa87240c5aed55/href">https://medium.com/media/5fee7a5ad33c3b0915fa87240c5aed55/href</a></iframe><p>It enables us to configure our adapter with existing <em>vocabulary</em> of RecyclerView.Adapter. So we don’t need to learn new concepts to implement/ handle the things related to RecyclerView.Adapters, such as binding ViewHolder, handling events like onClick ,onLongClick or something else.</p><p>So when we check how Kiel helps us:</p><h4>RecyclerView.Adapter Implementation becomes:</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28255%252C255%252C255%252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dval%252520adapter%252520%25253DadapterOf%25253CMessageViewState%25253E%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520register%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520layoutResource%252520%25253D%252520R.layout.adapter_message_text_item%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520viewHolder%252520%25253D%252520%25253A%25253ATextMessageViewHolder%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520onBindBindViewHolder%252520%25253D%252520%25257B%252520vh%25252C%252520_%25252C%252520text%252520-%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520vh.messageText.text%252520%25253D%252520text.text%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520vh.sentAt.text%252520%25253D%252520text.sentAt%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%29%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28255%25252C255%25252C255%25252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dval%25252520adapter%25252520%2525253DadapterOf%2525253CMessageViewState%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520register%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520layoutResource%25252520%2525253D%25252520R.layout.adapter_message_text_item%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520viewHolder%25252520%2525253D%25252520%2525253A%2525253ATextMessageViewHolder%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520onBindBindViewHolder%25252520%2525253D%25252520%2525257B%25252520vh%2525252C%25252520_%2525252C%25252520text%25252520-%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520vh.messageText.text%25252520%2525253D%25252520text.text%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520vh.sentAt.text%25252520%2525253D%25252520text.sentAt%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2d8ba1810153616c1defb4d022a3bc7/href">https://medium.com/media/f2d8ba1810153616c1defb4d022a3bc7/href</a></iframe><h4><strong>Handling Different View Types becomes:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-FUXlnR97whr-nHVgdcFEg.png" /></figure><h4>Diffing becomes:</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28255%252C255%252C255%252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520%252520%252520%252520%252520diff%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520areItemsTheSame%252520%25253D%252520%25257B%252520old%25252C%252520new%252520-%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520old.id%252520%25253D%25253D%252520%252520new.id%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520areContentsTheSame%252520%25253D%252520%25257B%252520old%25252C%252520new%252520-%25253E%252520old.item%252520%25253D%25253D%252520new.item%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%29&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28255%25252C255%25252C255%25252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520%25252520%25252520%25252520%25252520diff%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520areItemsTheSame%25252520%2525253D%25252520%2525257B%25252520old%2525252C%25252520new%25252520-%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520old.id%25252520%2525253D%2525253D%25252520%25252520new.id%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520areContentsTheSame%25252520%2525253D%25252520%2525257B%25252520old%2525252C%25252520new%25252520-%2525253E%25252520old.item%25252520%2525253D%2525253D%25252520new.item%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe1221a65b0e0d4fb6f1109da591cf87/href">https://medium.com/media/fe1221a65b0e0d4fb6f1109da591cf87/href</a></iframe><h4>Diffing with Payloads:</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28255%252C255%252C255%252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520diff%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520areItemsTheSame%252520%25253D%252520%25257B%252520old%25252C%252520new%252520-%25253E%252520old.id%252520%25253D%25253D%252520new.id%252520%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520areContentsTheSame%252520%25253D%252520%25257B%252520old%25252C%252520new%252520-%25253E%252520old%252520%25253D%25253D%252520new%252520%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520getChangePayload%252520%25253D%252520%25257B%252520oldItem%25252C%252520newItem%252520-%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520val%252520diffBundle%252520%25253D%252520Bundle%28%29%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520if%252520%28newItem.value%252520%21%25253D%252520oldItem.value%29%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520diffBundle.putDouble%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520KEY_VALUE%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520newItem.value%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%29%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520...%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520if%252520%28diffBundle.size%28%29%252520%25253D%25253D%2525200%29%252520null%252520else%252520diffBundle%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%29&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28255%25252C255%25252C255%25252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520diff%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520areItemsTheSame%25252520%2525253D%25252520%2525257B%25252520old%2525252C%25252520new%25252520-%2525253E%25252520old.id%25252520%2525253D%2525253D%25252520new.id%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520areContentsTheSame%25252520%2525253D%25252520%2525257B%25252520old%2525252C%25252520new%25252520-%2525253E%25252520old%25252520%2525253D%2525253D%25252520new%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520getChangePayload%25252520%2525253D%25252520%2525257B%25252520oldItem%2525252C%25252520newItem%25252520-%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520val%25252520diffBundle%25252520%2525253D%25252520Bundle%28%29%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520%28newItem.value%25252520%21%2525253D%25252520oldItem.value%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520diffBundle.putDouble%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520KEY_VALUE%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520newItem.value%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520...%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520%28diffBundle.size%28%29%25252520%2525253D%2525253D%252525200%29%25252520null%25252520else%25252520diffBundle%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e20dc317fe1351f307db94af0ac0ee1/href">https://medium.com/media/2e20dc317fe1351f307db94af0ac0ee1/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28255%252C255%252C255%252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520%252520%252520%252520%252520%252520%252520%252520%252520register%28%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520layoutResource%252520%25253D%252520...%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520viewHolder%252520%25253D%252520...%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520onBindViewHolder%252520%25253D%252520%25257B%252520vh%25252C%252520_%25252C%252520it%252520-%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520onBindViewHolderWithPayload%252520%25253D%252520%25257B%252520vh%25252C%252520_%25252C%252520it%25252C%252520payload%252520-%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25252F%25252F%252520we%252520can%252520get%252520the%252520payload%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25252F%25252F%252520which%252520is%252520provided%252520in%252520getPayload%252520in%252520diff.%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%29&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28255%25252C255%25252C255%25252C1%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520register%28%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520layoutResource%25252520%2525253D%25252520...%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520viewHolder%25252520%2525253D%25252520...%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520onBindViewHolder%25252520%2525253D%25252520%2525257B%25252520vh%2525252C%25252520_%2525252C%25252520it%25252520-%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520onBindViewHolderWithPayload%25252520%2525253D%25252520%2525257B%25252520vh%2525252C%25252520_%2525252C%25252520it%2525252C%25252520payload%25252520-%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520we%25252520can%25252520get%25252520the%25252520payload%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520which%25252520is%25252520provided%25252520in%25252520getPayload%25252520in%25252520diff.%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%29&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/023667f7c5d728087420a74fa85dc439/href">https://medium.com/media/023667f7c5d728087420a74fa85dc439/href</a></iframe><p>You can find Kiel:</p><p><a href="https://github.com/ibrahimyilmaz/kiel">ibrahimyilmaz/kiel</a></p><p>You can download:</p><pre>implementation &#39;me.ibrahimyilmaz:kiel:latestVersion&#39;</pre><p>Your ideas/feedbacks/PRs/Issues are greately appreciated.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a129882c1e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/build-better-and-clean-recyclerview-adapter-with-kiel-a129882c1e1">Build Better and Clean RecyclerView.Adapter With Kiel</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Communicating a Fragment With Other Fragments and Activities]]></title>
            <link>https://medium.com/swlh/communicating-a-fragment-with-other-fragments-and-activities-720925a7d9d1?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/720925a7d9d1</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Mon, 27 Jul 2020 18:26:52 GMT</pubDate>
            <atom:updated>2020-08-20T19:00:44.639Z</atom:updated>
            <content:encoded><![CDATA[<h4>When the communication between Fragment and Activity matters.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jr11yEQsmDd3x6cJ" /><figcaption>Photo by <a href="https://unsplash.com/@dinazina?utm_source=medium&amp;utm_medium=referral">Dina Lydia</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Fragments and Activities are the points with which our users directly interact. As the navigation between Activities is very expensive and cause performance problems, using single activity and many fragments in it for a work-flow/user-flow has become very popular way of developing Android App nowadays.</p><p><a href="https://medium.com/androiddevelopers/exceed-the-android-speed-limit-b73a0692abc1">Exceed the Android Speed Limit!</a></p><p>So communicating Fragment with other Fragments and Activity is one of important topics which we should take it seriously in order to develop maintainable Android application.</p><h3><strong>Communicating Fragment with Activity</strong></h3><p>Let’s firstly take a look at what Google recommends for the communication between Fragment and Activity:</p><blockquote>To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods to communicate with the Activity.</blockquote><p><a href="https://developer.android.com/training/basics/fragments/communicating#DefineInterface">Communicate with other fragments | Android Developers</a></p><h4>Fragment Interfaces</h4><p>We basically define the interface(which is the contract of the Fragment) in the Fragment and we implement this interface in the Activity. We glue them by using onAttachFragment of Activity or onAttach method of Fragment. That’s it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520HeadlinesFragment%252520%25253A%252520ListFragment%28%29%252520%25257B%25250A%252520%252520%252520%252520internal%252520var%252520callback%25253A%252520OnHeadlineSelectedListener%25250A%25250A%252520%252520%252520%252520fun%252520setOnHeadlineSelectedListener%28callback%25253A%252520OnHeadlineSelectedListener%29%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520this.callback%252520%25253D%252520callback%25250A%252520%252520%252520%252520%25257D%25250A%25250A%252520%252520%252520%252520%25252F%25252F%252520This%252520interface%252520can%252520be%252520implemented%252520by%252520the%252520Activity%25252C%252520parent%252520Fragment%25252C%25250A%252520%252520%252520%252520%25252F%25252F%252520or%252520a%252520separate%252520test%252520implementation.%25250A%252520%252520%252520%252520interface%252520OnHeadlineSelectedListener%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520fun%252520onArticleSelected%28position%25253A%252520Int%29%25250A%252520%252520%252520%252520%25257D%25250A%25250A%252520%252520%252520%252520%25252F%25252F%252520...%25250A%25257D%25250A%25250Aclass%252520MainActivity%252520%25253A%252520Activity%28%29%25252C%252520HeadlinesFragment.OnHeadlineSelectedListener%252520%25257B%25250A%252520%252520%252520%252520%25252F%25252F%252520...%25250A%25250A%252520%252520%252520%252520fun%252520onAttachFragment%28fragment%25253A%252520Fragment%29%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520%28fragment%252520is%252520HeadlinesFragment%29%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520fragment.setOnHeadlineSelectedListener%28this%29%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520HeadlinesFragment%25252520%2525253A%25252520ListFragment%28%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520internal%25252520var%25252520callback%2525253A%25252520OnHeadlineSelectedListener%2525250A%2525250A%25252520%25252520%25252520%25252520fun%25252520setOnHeadlineSelectedListener%28callback%2525253A%25252520OnHeadlineSelectedListener%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520this.callback%25252520%2525253D%25252520callback%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520This%25252520interface%25252520can%25252520be%25252520implemented%25252520by%25252520the%25252520Activity%2525252C%25252520parent%25252520Fragment%2525252C%2525250A%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520or%25252520a%25252520separate%25252520test%25252520implementation.%2525250A%25252520%25252520%25252520%25252520interface%25252520OnHeadlineSelectedListener%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520fun%25252520onArticleSelected%28position%2525253A%25252520Int%29%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520...%2525250A%2525257D%2525250A%2525250Aclass%25252520MainActivity%25252520%2525253A%25252520Activity%28%29%2525252C%25252520HeadlinesFragment.OnHeadlineSelectedListener%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520...%2525250A%2525250A%25252520%25252520%25252520%25252520fun%25252520onAttachFragment%28fragment%2525253A%25252520Fragment%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520%28fragment%25252520is%25252520HeadlinesFragment%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520fragment.setOnHeadlineSelectedListener%28this%29%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/945cace8bc5ebcda1e6eaf7f72d73df3/href">https://medium.com/media/945cace8bc5ebcda1e6eaf7f72d73df3/href</a></iframe><p>This is not directly recommended by Google but we can overuse shared ViewModel between Fragment and Activity to glue them.</p><h4>Shared ViewModel</h4><p>We basically create an extra ViewModel with its LiveData variables for the Fragment to share its state with the Activity.</p><p>TLDR; as its observer is Activity, please don’t forget to use activity life cycle when you create ViewModel in the Fragment. That’s why activityViewModels() builder function in this example is used.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520HeadlinesFragmentSharedViewModel%252520%25253A%252520ViewModel%28%29%25257B%25250A%252520%252520%252520%252520%252520val%252520changeHeadline%252520%25253A%252520LiveData%25253CInt%25253E%252520get%28%29%252520%25253D%252520_changeHeadline%25250A%252509%252520private%252520val%252520_changeHeadline%252520%25253D%252520LiveDataEvent%25253CInt%25253E%25250A%252509%25250A%252520%252520%252520%252520%252520fun%252520onArticleSelected%28position%25253AInt%29%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520_changeHeadline.value%252520%25253D%252520position%25250A%252520%252520%252520%252520%252520%25257D%25250A%25250A%25257D%25250A%25250Aclass%252520HeadlinesFragment%252520%25253A%252520ListFragment%28%29%252520%25257B%25250A%252520%252520%252520%252520%252520internal%252520var%252520sharedViewModel%25253A%252520HeadlinesFragmentSharedViewModel%252520by%252520activityViewModels%28%29%25250A%25250A%252520%252520%252520%252520%252520%25252F%25252F%252520we%252520call%252520sharedViewModel.onArticleSelected%28%29%252520in%252520somewhere%25250A%25250A%252520%25250A%25257D%25250A%25250Aclass%252520MainActivity%252520%25253A%252520Activity%28%29%252520%25257B%25250A%252520%252520%252520%252520%252520internal%252520var%252520headlinesFragmentSharedViewModel%25253A%252520HeadlinesFragmentSharedViewModel%252520by%252520viewModels%28%29%25250A%25250A%252520%252520%25250A%252520%252520%252520%252520%252520fun%252520onCreate%28savedInstanceState%25253ABundle%25253F%29%25257B%25250A%252520%252520%252520%252520%252520%252520%252520...%25250A%252520%252520%252520%252520%252520%252520%252520%25252F%25252F%252520we%252520should%252520observe%25250A%252520%252520%252520%252520%252520%252520%252520sharedViewModel.changeHeadline.observe%28this%25252CObserver%28%25253A%25253AonHeadlineChanged%29%29%25250A%252520%252520%252520%252520%252520%25257D%25250A%252520%252520%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520HeadlinesFragmentSharedViewModel%25252520%2525253A%25252520ViewModel%28%29%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520val%25252520changeHeadline%25252520%2525253A%25252520LiveData%2525253CInt%2525253E%25252520get%28%29%25252520%2525253D%25252520_changeHeadline%2525250A%25252509%25252520private%25252520val%25252520_changeHeadline%25252520%2525253D%25252520LiveDataEvent%2525253CInt%2525253E%2525250A%25252509%2525250A%25252520%25252520%25252520%25252520%25252520fun%25252520onArticleSelected%28position%2525253AInt%29%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520_changeHeadline.value%25252520%2525253D%25252520position%2525250A%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%2525257D%2525250A%2525250Aclass%25252520HeadlinesFragment%25252520%2525253A%25252520ListFragment%28%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520internal%25252520var%25252520sharedViewModel%2525253A%25252520HeadlinesFragmentSharedViewModel%25252520by%25252520activityViewModels%28%29%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520we%25252520call%25252520sharedViewModel.onArticleSelected%28%29%25252520in%25252520somewhere%2525250A%2525250A%25252520%2525250A%2525257D%2525250A%2525250Aclass%25252520MainActivity%25252520%2525253A%25252520Activity%28%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520internal%25252520var%25252520headlinesFragmentSharedViewModel%2525253A%25252520HeadlinesFragmentSharedViewModel%25252520by%25252520viewModels%28%29%2525250A%2525250A%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520fun%25252520onCreate%28savedInstanceState%2525253ABundle%2525253F%29%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520...%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252F%25252520we%25252520should%25252520observe%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520sharedViewModel.changeHeadline.observe%28this%2525252CObserver%28%2525253A%2525253AonHeadlineChanged%29%29%2525250A%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bccdebb6b192f62f8ffeeb001770314/href">https://medium.com/media/0bccdebb6b192f62f8ffeeb001770314/href</a></iframe><p><strong>Listener over Shared ViewModel</strong></p><ol><li>) Listener has less code than Shared View Model.</li><li>) Listener forces its contract to the Activity, so Activity cannot skip any point otherwise we see unimplemented methods.</li><li>) Fragment may not live without its Listener(if it is forced)</li></ol><p><strong>Shared ViewModel over Listener</strong></p><ol><li>) Shared ViewModel is testable. We can easily write unit tests for them.</li><li>) Shared ViewModel can have filtering the notification logic in it.</li><li>) Activity does not need to observe all of events of the Fragment.</li></ol><p>Both of them have pros and cons, we should decide one of them according to our system requirements. Both of them have no major advantages against each other in the Fragment/Activity communication perspective.</p><p>For example: if this ViewModel is used by many Fragments for Navigation purpose. They can both use same interface Listener or Shared ViewModel .</p><h3>Communicating Fragment with Fragment</h3><p>Again let’s firstly take a look at what Google recommends:</p><blockquote>The recommended way to communicate between fragments is to create a shared <a href="https://developer.android.com/reference/androidx/lifecycle/ViewModel">ViewModel</a>. Both fragments can access the ViewModel through their containing Activity. The Fragments can update data within the ViewModel and if the data is exposed using <a href="https://developer.android.com/reference/androidx/lifecycle/LiveData">LiveData</a>, the new state will be pushed to the other fragment as long as it is observing the LiveData from the ViewModel. To see how to implement this kind of communication, read the &#39;Share data between Fragments&#39; section in the <a href="https://developer.android.com/topic/libraries/architecture/viewmodel">ViewModel guide</a>.</blockquote><p><a href="https://developer.android.com/training/basics/fragments/communicating">https://developer.android.com/training/basics/fragments/communicating</a></p><p><strong>Shared ViewModels</strong></p><p>The fragments share the data or the state between each other ViewModel living under the lifecycle of Activity.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%28171%252C%2520184%252C%2520195%252C%25201%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520RegistrationSharedViewModel%252520%25253A%252520ViewModel%28%29%25257B%25250A%252520%252520%252520%252520%252520val%252520email%252520%25253A%252520LiveData%25253CString%25253E%252520get%28%29%252520%25253D%252520_changeHeadline%25250A%252509%252520private%252520val%252520_email%252520%25253D%252520MutableLiveData%25253CInt%25253E%25250A%252509%25250A%252520%252520%252520%252520%252520fun%252520onEmailSelected%28email%25253AString%29%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520_email.value%252520%25253D%252520email%25250A%252520%252520%252520%252520%252520%25257D%25250A%25257D%25250A%25250Aclass%252520EnterEmailFragment%252520%25253A%252520Fragment%28%29%252520%25257B%25250A%252520%252520%252520%252520%252520internal%252520var%252520sharedViewModel%25253A%252520RegistrationSharedViewModel%252520by%252520activityViewModels%28%29%25250A%25257D%25250A%25250Aclass%252520ConfirmEmailFragment%252520%25253A%252520Fragment%28%29%252520%25257B%25250A%252520%252520%252520%252520%252520internal%252520var%252520sharedViewModel%25253A%252520RegistrationSharedViewModel%252520by%252520activityViewModels%28%29%25250A%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%28171%25252C%252520184%25252C%252520195%25252C%2525201%29%26t%3Dmonokai%26wt%3Dnone%26l%3Dtext%25252Fx-kotlin%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520RegistrationSharedViewModel%25252520%2525253A%25252520ViewModel%28%29%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520val%25252520email%25252520%2525253A%25252520LiveData%2525253CString%2525253E%25252520get%28%29%25252520%2525253D%25252520_changeHeadline%2525250A%25252509%25252520private%25252520val%25252520_email%25252520%2525253D%25252520MutableLiveData%2525253CInt%2525253E%2525250A%25252509%2525250A%25252520%25252520%25252520%25252520%25252520fun%25252520onEmailSelected%28email%2525253AString%29%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520_email.value%25252520%2525253D%25252520email%2525250A%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%2525257D%2525250A%2525250Aclass%25252520EnterEmailFragment%25252520%2525253A%25252520Fragment%28%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520internal%25252520var%25252520sharedViewModel%2525253A%25252520RegistrationSharedViewModel%25252520by%25252520activityViewModels%28%29%2525250A%2525257D%2525250A%2525250Aclass%25252520ConfirmEmailFragment%25252520%2525253A%25252520Fragment%28%29%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520internal%25252520var%25252520sharedViewModel%2525253A%25252520RegistrationSharedViewModel%25252520by%25252520activityViewModels%28%29%2525250A%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/37efc11595a689e839ea902afa888ec1/href">https://medium.com/media/37efc11595a689e839ea902afa888ec1/href</a></iframe><p>I would like to mention other ways for Fragment/Fragment communication like EventBus etc. However, I find these other ways event can’t help as sharedViewModel but also can cause many boiler plate/ lifecycle issues and code complexities.</p><p>Happy Coding</p><p><a href="https://github.com/ibrahimyilmaz">ibrahimyilmaz - Overview</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=720925a7d9d1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/communicating-a-fragment-with-other-fragments-and-activities-720925a7d9d1">Communicating a Fragment With Other Fragments and Activities</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Hilt with FragmentFactory]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://cs-ibrahimyilmaz.medium.com/how-to-use-hilt-with-fragmentfactory-dab364ddb5e9?source=rss-bfa5f552d0f6------2"><img src="https://cdn-images-1.medium.com/max/2600/0*SWVZJg19nPIIilns" width="3784"></a></p><p class="medium-feed-snippet">Hi Android Fellas,</p><p class="medium-feed-link"><a href="https://cs-ibrahimyilmaz.medium.com/how-to-use-hilt-with-fragmentfactory-dab364ddb5e9?source=rss-bfa5f552d0f6------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://cs-ibrahimyilmaz.medium.com/how-to-use-hilt-with-fragmentfactory-dab364ddb5e9?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/dab364ddb5e9</guid>
            <category><![CDATA[hilts]]></category>
            <category><![CDATA[dagger-hilt]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Fri, 17 Jul 2020 23:40:33 GMT</pubDate>
            <atom:updated>2020-07-17T23:44:25.269Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Kotlin Flows Producer-Consumer Design Pattern Implementation]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://cs-ibrahimyilmaz.medium.com/kotlin-flows-producer-consumer-design-pattern-implementation-1443fc6881b5?source=rss-bfa5f552d0f6------2"><img src="https://cdn-images-1.medium.com/max/2600/0*DZSVRT5CQG93BWC3" width="5212"></a></p><p class="medium-feed-snippet">In this post, I would like to demonstrate a basic producer - consumer model that uses Kotlin Flows (consumer is faster than producer).</p><p class="medium-feed-link"><a href="https://cs-ibrahimyilmaz.medium.com/kotlin-flows-producer-consumer-design-pattern-implementation-1443fc6881b5?source=rss-bfa5f552d0f6------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://cs-ibrahimyilmaz.medium.com/kotlin-flows-producer-consumer-design-pattern-implementation-1443fc6881b5?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/1443fc6881b5</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[flow]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 13:08:13 GMT</pubDate>
            <atom:updated>2020-06-27T13:08:13.759Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Liskov Substitution Principle]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://cs-ibrahimyilmaz.medium.com/liskov-substitution-principle-63fe3ba5d9cf?source=rss-bfa5f552d0f6------2"><img src="https://cdn-images-1.medium.com/max/2600/0*hS2ogVt8BP9ROLyF" width="2678"></a></p><p class="medium-feed-snippet">I would like to talk about Liskov Substitution Principle in this article and share my daily work experience about it.</p><p class="medium-feed-link"><a href="https://cs-ibrahimyilmaz.medium.com/liskov-substitution-principle-63fe3ba5d9cf?source=rss-bfa5f552d0f6------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://cs-ibrahimyilmaz.medium.com/liskov-substitution-principle-63fe3ba5d9cf?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/63fe3ba5d9cf</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Sun, 14 Jun 2020 18:55:21 GMT</pubDate>
            <atom:updated>2020-06-15T12:21:10.829Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Open Closed Principle (OCP)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://cs-ibrahimyilmaz.medium.com/open-closed-principle-ocp-14746b35d72?source=rss-bfa5f552d0f6------2"><img src="https://cdn-images-1.medium.com/max/2600/0*4dMQHBZGB-jrYG31" width="7680"></a></p><p class="medium-feed-snippet">In the O part of SOLID, I want to start again asserting. Now I want to assert that O is the most misunderstood design pattern and practise&#x2026;</p><p class="medium-feed-link"><a href="https://cs-ibrahimyilmaz.medium.com/open-closed-principle-ocp-14746b35d72?source=rss-bfa5f552d0f6------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://cs-ibrahimyilmaz.medium.com/open-closed-principle-ocp-14746b35d72?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/14746b35d72</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[solid]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 13:54:20 GMT</pubDate>
            <atom:updated>2020-06-08T13:54:58.709Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Single Responsibility Principle(SRP)]]></title>
            <link>https://cs-ibrahimyilmaz.medium.com/single-responsibility-principle-srp-8b6d5e346651?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b6d5e346651</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Wed, 03 Jun 2020 13:20:30 GMT</pubDate>
            <atom:updated>2020-06-03T13:20:30.972Z</atom:updated>
            <content:encoded><![CDATA[<p>I want to start asserting that Single Responsibility Principle is the most important design pattern to be followed by a Developer who wants to be better Developer and a Company(company may force it to its developers) which wants to have more maintainable Product.</p><p>I would like to share my experience about Single Responsibility Principle in my daily work experience and I would like to use SRP instead of Single Responsibility Principle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZkQc6I9_dxI81LR1" /><figcaption>Photo by <a href="https://unsplash.com/@mattykwong1?utm_source=medium&amp;utm_medium=referral">Matthew Kwong</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Lets start explaining what <strong>SRP</strong> is:</p><p>Its Creator <em>Uncle Bob</em> defines that:</p><blockquote>A class should have one, and only one, reason to change.</blockquote><p>It seems very easy, so we can easily say that we create a class or method which focuses on one subject. That’s all.</p><p>Yes! It is correct.</p><p>If someone asks about <strong>SRP</strong>, we can explain it like this.</p><p>The main point is</p><ul><li>How do you apply this pattern correctly?</li><li>How can we measure the level of <strong>SRP</strong> for a class?</li><li>How do we validate our code according to this?</li></ul><p>The answer is <strong>Unit Test</strong>.</p><p>If there is no Unit Test in your project, I am sorry but <strong>SRP</strong> is nothing more than its definition for that project. We only know the definition and claim that we apply <strong>SRP</strong>. But we can’t really assert that idea.</p><p><strong>First Things First</strong></p><p>We should have Unit Test, if we don’t have we should start to implement Unit Test and cover our code as much as we can.</p><p>We can ask why we should have unit test for this or how it can help us to validate if we apply <strong>SRP</strong> good or bad.</p><blockquote>The answer: How hard the testing of that class is?</blockquote><p>If we can’t write unit tests easily because of very complex mockings or if our unit test contains logic of the mocked dependency, we can say that the class under test has very fundamental problems. We will probably find us debugging that code and trying to discover the bug!(even if we have unit tests!).</p><blockquote>TLDR; the easier and cleaner we test, the better single responsibility we apply.</blockquote><p>For example:</p><p>I want to start with very bad code and wanna show how this evolves the better one.</p><p>We have a repository class which fetches the news from an API and caches them for 10 minutes.</p><pre>class NewsRepository(val retrofit:Retrofit){</pre><pre>   private val newsService =   retrofit.create(NewsService::class.java)<br>private val newsCache = NewsCache()</pre><pre> suspend fun getNews():List&lt;News&gt;{</pre><pre>   val cachedNewsList = newsCache.getNewsList()</pre><pre>   if(cachedNewsList != null) return cachedNewsList</pre><pre>   val newsList = newsService.fetchNewsList()<br>newsCache.cacheNewsList(newsList)</pre><pre>   return newsList<br>}</pre><pre>}<br>...</pre><pre>@Test<br>fun shouldGetNewsListSuccessfully(){<br>   // GIVEN<br>   val newsService = mock&lt;NewsService&gt;() <br>   val newsList = mock&lt;List&lt;News&gt;&gt;()         <br>   whenever(newsService.fetchNewsList()).doReturn(newsList)<br>   whenever(retrofit.create(any())).thenReturn(newsService)<br>   val repository = NewsRepository(retrofit)<br>   <br>   // WHEN<br>   val result = repository.getNews()</pre><pre>   // THEN<br>   assertThat(result).isEqualTo(newsList)</pre><pre>}</pre><pre>@Test(expected = SomeException::class)<br>fun shouldThrowExceptionWhenFetchingListFails(){<br>  // GIVEN<br>   val newsService = mock&lt;NewsService&gt;() <br>   val newsList = mock&lt;List&lt;News&gt;&gt;()         <br>   whenever(newsService.fetchNewsList()).doThrowException(SomeException::class)<br>   whenever(retrofit.create(any())).thenReturn(newsService)<br>   val repository = NewsRepository(retrofit)<br>   // WHEN<br>   val result = repository.getNews()<br>}</pre><p>As we can see:</p><ul><li>we can’t do any meaningful assertion.</li><li>we struggle mocking news service</li><li>we can’t test cache at the moment</li></ul><p>But we have a class which focuses on orchestrating service and cache to provide news list to its client.</p><p>It seems correct! and I have unit tests which covers 50% of code!!!</p><p>To test this class we should have more meaningful assertions than</p><pre>fun shouldGetNewsListSuccessfully()</pre><p>Because this test totally does not make any sense. Our assertions can be:</p><pre>should_fetch_news_list_from_the_news_service_when_it_is_not_cached<br>should_return_news_list_from_cache_when_it_is_cached<br>should_not_fetch_news_news_list_from_news_service_when_it_is_cached<br>should_cache_news_list_when_news_list_is_fetched_from_news_service<br>should_throw_exception_when_news_service_fails_in_fetching_the_service</pre><p>If we assert this tests, it means we know better about our NewsRepository.</p><pre>should_fetch_news_list_from_the_news_service_when_it_is_not_cached</pre><p>I should be able to mock <strong>NewsCache </strong>to always fetch data from the news service. But when I look at NewsRepository, it creates this, so we should remove this responsibility from <strong>NewsRepository </strong>by giving this as a constructor parameter.</p><pre>class NewsRepository(<br>private val retrofit:Retrofit,<br>private val newsCache:NewsCache<br>){</pre><pre> private val newsService =   retrofit.create(NewsService::class.java)</pre><pre>  suspend fun getNews():List&lt;News&gt;{</pre><pre>    val cachedNewsList = newsCache.getNewsList()</pre><pre>    if(cachedNewsList != null) return cachedNewsList</pre><pre>    val newsList = newsService.fetchNewsList()<br>    newsCache.cacheNewsList(newsList)</pre><pre>    return newsList<br>}</pre><pre>}</pre><p>this test would be:</p><pre>   whenever(newsCache.get()).thenReturn(null)<br>   val newsService = mock&lt;NewsService&gt;() <br>   val newsList = mock&lt;List&lt;News&gt;&gt;()         <br>   whenever(newsService.fetchNewsList()).doReturn(newsList)<br>   whenever(retrofit.create(any())).thenReturn(newsService)<br>   val repository = NewsRepository(retrofit,newsCache)</pre><pre>   // WHEN<br>   val result = repository.getNews()</pre><pre>   // THEN<br>   verify(newsService).fetchNewsList()</pre><p>still we are suffering from newsService creation even if we are able to assert easily. Lets move this responsibility from <strong>NewsRepository </strong>and provide <strong>NewsService</strong> to it.</p><pre>class NewsRepository(<br>private val newsService:NewsService,<br>private val newsCache:NewsCache<br>){</pre><pre>suspend fun getNews():List&lt;News&gt;{</pre><pre>val cachedNewsList = newsCache.getNewsList()</pre><pre>if(cachedNewsList != null) return cachedNewsList</pre><pre>val newsList = newsService.fetchNewsList()<br>    newsCache.cacheNewsList(newsList)</pre><pre>return newsList<br>}</pre><pre>}</pre><p>Now our test is:</p><pre>val newsService = mock&lt;NewsService&gt;()     <br>   val newsCache = mock&lt;NewsCache&gt;()     <br>   whenever(newsCache.get()).thenReturn(null)   <br>   val repository = NewsRepository(newsService,newsCache)</pre><pre>   // WHEN<br>   val result = repository.getNews()</pre><pre>   // THEN<br>   verify(newsService).fetchNewsList()</pre><p>and we can easily implement remanining tests:</p><pre>should_return_news_list_from_cache_when_it_is_cached()</pre><pre>// GIVEN  <br>  val newsService = mock&lt;NewsService&gt;()     <br>  val newsCache = mock&lt;NewsCache&gt;()   <br>   val cachedNewsList = mock&lt;List&lt;News&gt;&gt;()  <br>   whenever(newsCache.get()).thenReturn(cachedNewsList)   <br>   val repository = NewsRepository(newsService,newsCache)</pre><pre>// WHEN<br>   val result = repository.getNews()</pre><pre>// THEN<br>   assertThat(result).isEqualTo(cachedNewsList)</pre><pre><br>should_not_fetch_news_news_list_from_news_service_when_it_is_cached</pre><pre>// GIVEN  <br>  val newsService = mock&lt;NewsService&gt;()     <br>  val newsCache = mock&lt;NewsCache&gt;()   <br>   val cachedNewsList = mock&lt;List&lt;News&gt;&gt;()  <br>   whenever(newsCache.get()).thenReturn(cachedNewsList)   <br>   val repository = NewsRepository(newsService,newsCache)</pre><pre>// WHEN<br>   val result = repository.getNews()</pre><pre>// THEN<br>   verify(newsService,never()).fetchNewsList()</pre><pre>should_cache_news_list_when_news_list_is_fetched_from_news_service</pre><pre>   val newsService = mock&lt;NewsService&gt;()     <br>   val newsCache = mock&lt;NewsCache&gt;() <br>   val newsList= mock&lt;List&lt;News&gt;&gt;()    <br>   whenever(newsCache.get()).thenReturn(null) <br>   whenever(newsService.fetchNewsList()).thenReturn(newsList)  <br>   val repository = NewsRepository(newsService,newsCache)</pre><pre>// WHEN<br>   val result = repository.getNews()</pre><pre>// THEN<br>   verify(newsCache).cacheNews(newsList)</pre><pre><br>should_throw_exception_when_news_service_fails_in_fetching_the_service</pre><pre>val newsService = mock&lt;NewsService&gt;()     <br>   val newsCache = mock&lt;NewsCache&gt;() <br>   val newsList= mock&lt;List&lt;News&gt;&gt;()    <br>   whenever(newsCache.get()).thenReturn(null) <br>   whenever(newsService.fetchNewsList()).thenThrow(SomeExceptionException())  <br>   val repository = NewsRepository(newsService,newsCache)</pre><pre>// WHEN<br>   val result = repository.getNews()</pre><p>So we can say that:</p><ul><li>NewsRepository class is very simple at the moment and very easy to test exhaustively.</li><li>Service and Cache creation is outside of Repository, so we can configure them however we want.</li><li>NewsRepository is more understandable, small and does not have extra details which is not important for NewsRepository. NewsService, NewsCache have client-service relationship with NewsRepository so we have low coupled and highly-cohesive classes.</li></ul><p>Enjoy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b6d5e346651" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin Coroutine Context]]></title>
            <link>https://cs-ibrahimyilmaz.medium.com/kotlin-coroutine-context-305af212c713?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/305af212c713</guid>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[kotlin-beginners]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Sun, 08 Dec 2019 16:22:42 GMT</pubDate>
            <atom:updated>2019-12-13T09:23:20.439Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is the CoroutineContext?</h3><blockquote><strong>If you can’t</strong> explain it <strong>simply</strong>, <strong>you don’t understand</strong> it well enough. — Albert Einstein</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SPz3KWbaqjh4mTvq" /><figcaption>Photo by <a href="https://unsplash.com/@rawfilm?utm_source=medium&amp;utm_medium=referral">RawFilm</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What is exactly this CoroutineContext?</h3><p>To give very simple question what exactly this CoroutineContext is, the first place that we should look kotlinlang.org.</p><p><a href="https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html">Coroutine Context and Dispatchers</a></p><p>It is written there:</p><pre>Coroutines always execute in some context represented by a value of the <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-coroutine-context/">CoroutineContext</a> type, defined in the Kotlin standard library</pre><blockquote>We understand that the code in the coroutine is executed in a CoroutineContext. So we can say that CoroutineContext provide us required execution environment to run/execute our code asyncronously.</blockquote><h3>What does this execution environment have?</h3><p>We can easily run coroutine by using launch/async . We can reach CoroutineContext in the functional scope of launch/async by using coroutineContext . So let’s print and see it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fpl.kotl.in%2FPTG58LDXs%3Ftheme%3Ddarcula%26from%3D12%26to%3D14%26readOnly%3Dtrue&amp;src=https%3A%2F%2Fpl.kotl.in%2FPTG58LDXs%3Ftheme%3Ddarcula%26from%3D12%26to%3D14%26readOnly%3Dtrue&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/462c7b9fc126a8518481399c882f2a29/href">https://medium.com/media/462c7b9fc126a8518481399c882f2a29/href</a></iframe><p>Output:</p><pre>launch: [</pre><pre>CoroutineId(2), </pre><pre>&quot;coroutine#2&quot;:StandaloneCoroutine{Active}@5eb5c224[or BlockingCoroutine5eb5c224],</pre><pre>BlockingEventLoop@53e25b76,</pre><pre>DefaultDispatcher{In Android, we can see this}</pre><pre>]</pre><p>Let’s take deep look at what this output means:</p><h4>CoroutineId:</h4><p>It is only created for debugging purpose.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b79c97ec9af35b8ab3fbd19987b2e463/href">https://medium.com/media/b79c97ec9af35b8ab3fbd19987b2e463/href</a></iframe><p>And here we can see one very important thing it is</p><pre>val combined = couroutineContext + context</pre><p>We will take a look at this deeply below.</p><h4>StandaloneCoroutine:</h4><p>As we have used launch method without Start parameter, StandaloneCoroutine is created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31f1d4e867060a0b396bcc7b6bb372fe/href">https://medium.com/media/31f1d4e867060a0b396bcc7b6bb372fe/href</a></iframe><p>And here we can see three major things to understand CoroutineContext better. First one is newContext creation. newCoroutineContext is created by using the given context. Second one is that new context is used in coroutine creation. And the last one is that our block is given to the created coroutine. After checking <em>BlockingEventLoop, </em>we will return this point again below.</p><blockquote>We can see the answer of what :the block which is suspend function in CoroutineScope .</blockquote><h4>BlockingEventLoop:</h4><p>This class is defined in EventLoop.kt</p><blockquote>kotlinx-coroutines-core/jvm/src/EventLoop.kt</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1536b2ec4f45604aa6a17f446558066f/href">https://medium.com/media/1536b2ec4f45604aa6a17f446558066f/href</a></iframe><p>We can only see here thread When we search where createEventLoop method is defined. It is defined in:</p><blockquote>kotlinx-coroutines-core/common/src/EventLoop.common.kt</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57c641a6cee4df233cf962c53534758b/href">https://medium.com/media/57c641a6cee4df233cf962c53534758b/href</a></iframe><p>eventLoop is called by related environment. You can see it for JVM</p><blockquote>kotlinx-coroutines-core/jvm/src/Builders.kt</blockquote><p>And it is coming from runBlocking because launch in our example runs on runBlocking block.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fpl.kotl.in%2FPTG58LDXs%3Ftheme%3Ddarcula%26from%3D11%26readOnly%3Dtrue%26to%3D15&amp;src=https%3A%2F%2Fpl.kotl.in%2FPTG58LDXs%3Ftheme%3Ddarcula%26from%3D11%26readOnly%3Dtrue%26to%3D15&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/85bd196eaca7cf7591a9d0e46943a728/href">https://medium.com/media/85bd196eaca7cf7591a9d0e46943a728/href</a></iframe><p>This explains the code above about related to coroutineContext copy:</p><pre>val combined = couroutineContext + context</pre><p>As runBlocking is the parent coroutine of the launch and its context is copied.</p><h4>DefaultDispatcher:</h4><p>It is defined in:</p><blockquote>kotlinx-coroutines-core/jvm/src/Dispatchers.kt</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/782cfab01500eee5b0ac01b797bd5870/href">https://medium.com/media/782cfab01500eee5b0ac01b797bd5870/href</a></iframe><p>This is mainly about where our coroutine works.</p><p>With the help of the ouput of very simple print of the CoroutineContext in launch , we understand what it is.</p><pre>launch: [</pre><pre>CoroutineId(2),</pre><pre>&quot;coroutine#2&quot;:StandaloneCoroutine{Active}@5eb5c224[or BlockingCoroutine5eb5c224],</pre><pre>BlockingEventLoop@53e25b76,</pre><pre>DefaultDispatcher{In Android, we can see this}</pre><pre>]</pre><p>Let’s take a look at CoroutineContext.kt :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d903f1e14008f80470a9d37a429061a/href">https://medium.com/media/4d903f1e14008f80470a9d37a429061a/href</a></iframe><p>We see Element class which is also CoroutineContext. When we search &quot;CoroutineContext.Element&quot; in the <a href="https://github.com/Kotlin/kotlinx.coroutines">Kotlin/kotlinx.coroutines</a>. We find:</p><ul><li>ContinuationInterceptor -&gt; interface</li><li>ThreadContextElement -&gt; interface</li><li>Job -&gt; interface</li><li>AbstractCoroutineContextElement -&gt; abstract class</li></ul><p>If we check their documentations and child classes/interfaces:</p><h3>ContinuationInterceptor:</h3><p>As it is experimental, we will skip this but</p><pre>private class <strong>ContinuationInterceptorMigration</strong>(val interceptor: ExperimentalContinuationInterceptor) : ContinuationInterceptor {</pre><pre>private class <strong>ExperimentalContinuationInterceptorMigration</strong>(val interceptor: ContinuationInterceptor) : ExperimentalContinuationInterceptor {</pre><p>it is called in:</p><pre>public fun CoroutineContext.toExperimentalCoroutineContext(): ExperimentalCoroutineContext {</pre><h3>ThreadContextElement:</h3><p><strong>CoroutineId</strong>: It is used debug purpose to follow coroutine by its id. we can find the details in:</p><blockquote>public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext):CoroutineContext {</blockquote><p><strong>ThreadLocalElement: </strong>It is used for creating thread local elements. As child Coroutine copy the context of the parent. We can pass parameter to child element with this.</p><pre><em>* val myThreadLocal = ThreadLocal&lt;String?&gt;()<br>* ...<br>* println(myThreadLocal.get()) // Prints &quot;null&quot;<br>* launch(Dispatchers.Default + myThreadLocal.asContextElement(value = &quot;foo&quot;)) {<br>*   println(myThreadLocal.get()) // Prints &quot;foo&quot;<br>*   withContext(Dispatchers.Main) {<br>*     println(myThreadLocal.get()) // Prints &quot;foo&quot;, but it&#39;s on UI thread<br>*   }<br>* }<br>* println(myThreadLocal.get()) // Prints &quot;null&quot;</em></pre><p>But the parent can’t reach the local variables of its children.</p><pre><em>* The context element does not track modifications of the thread-local variable, for example:<br>*<br>* ```<br>* myThreadLocal.set(&quot;main&quot;)<br>* withContext(Dispatchers.Main) {<br>*   println(myThreadLocal.get()) // Prints &quot;main&quot;<br>*   myThreadLocal.set(&quot;UI&quot;)<br>* }<br>* println(myThreadLocal.get()) // Prints &quot;main&quot;, not &quot;UI&quot;<br>* ```</em></pre><h3><strong>Job:</strong></h3><p>I think the documentation of Job is very very clear. Job’s main responsibility is to manage coroutine cancellation.</p><pre><em>* A background job. Conceptually, a job is a cancellable thing with a life-cycle that<br>* culminates in its completion.<br>*<br>* Jobs can be arranged into parent-child hierarchies where cancellation<br>* of a parent leads to immediate cancellation of all its [children]. Failure or cancellation of a child<br>* with an exception other than [CancellationException] immediately cancels its parent. This way, a parent<br>* can [cancel] its own children (including all their children recursively) without cancelling itself.<br>*<br>* The most basic instances of [Job] are created with [launch][CoroutineScope.launch] coroutine builder or with a<br>* `Job()` factory function. By default, a failure of any of the job&#39;s children leads to an immediate failure<br>* of its parent and cancellation of the rest of its children. This behavior can be customized using [SupervisorJob].<br>*<br>* Conceptually, an execution of the job does not produce a result value. Jobs are launched solely for their<br>* side-effects. See [Deferred] interface for a job that produces a result.</em></pre><h4><strong>NonCancellable:</strong></h4><p>Its documentation also explains what it is very very clearly.</p><pre><em>/**<br> * A non-cancelable job that is always [active][Job.isActive]. It is designed for [withContext] function<br> * to prevent cancellation of code blocks that need to be executed without cancellation.<br> *<br> * Use it like this:<br> * ```<br> * withContext(NonCancellable) {<br> *     // this code will not be cancelled<br> * }<br> * ```<br> */</em></pre><h4>Deferred:</h4><p>Deferred is a Job with return value. We generally see this with async{ } . Its documentation is very very clear.</p><pre><em>/**<br> * Creates a coroutine and returns its future result as an implementation of [Deferred].<br>...</em></pre><h4>CompletableJob:</h4><p>It is a interface to add complete functionality to our Job. It is used in JobImpl.</p><h4>JobImpl:</h4><p>It is concerete implementation of Job concept that we generally use to orchestrate cancellation of our coroutines.</p><h3>AbstractCoroutineContextElement:</h3><p>It is base class of :</p><ul><li>CoroutineName</li><li>CoroutineId</li><li>NonCancellable(Job)</li><li>CoroutineDispatcher</li><li>CoroutineId</li></ul><h4>CoroutineName:</h4><p>We can specify the name of coroutine for debugging purpose.</p><h4>CoroutineDispatcher:</h4><p>As we talked about it before, we use this to specify where the coroutine will work. There are 4 dispatchers, which are Default, Main, IO and Unconfined.</p><blockquote><strong>Default</strong>: It is recommended to use CPU intensive works such as very costly mathematical computations.</blockquote><blockquote><strong>IO</strong>: It is recommended to used IO Intensive works such as Network Call, File IO or socket IO</blockquote><blockquote><strong>Main</strong>: It is recommended to use Main thread operations such as updating UI.</blockquote><blockquote><strong>Unconfined</strong>: It is not confined for any use-case. It is recommend to use Testing purpose.</blockquote><p>To sum up, we can clearly see that CoroutineContext is environment on which our Coroutines run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*PF93e3va3mPhJHtMHO_J1A.png" /><figcaption>Simply Coroutine Context</figcaption></figure><p>I hope that I explain clearly what CoroutineContext is.</p><p>Please comment If there is something not clear. In upcoming posts, I will try to mention about Coroutine cancellation and exceptions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=305af212c713" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[With Kotlin more readable Robot Pattern]]></title>
            <link>https://cs-ibrahimyilmaz.medium.com/with-kotlin-more-readable-robot-pattern-a98db0c10b92?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a98db0c10b92</guid>
            <category><![CDATA[espresso]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Mon, 25 Nov 2019 22:17:52 GMT</pubDate>
            <atom:updated>2019-11-27T20:23:45.720Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P80r7188UKdrvDWs" /><figcaption>Photo by <a href="https://unsplash.com/@achernenko?utm_source=medium&amp;utm_medium=referral">Alex Chernenko</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this post, I want to explain how we can make our `androidTests`with Robot Pattern more readable. In order to do this, I want to explain very shortly what the test bed is is and what Robot Pattern is, subsequently.</p><h3>Test Bed:</h3><p>Even Jimm Carry made advertisement of Tempur Bed, the most comfortable bed in the world for Software Developers is Unit Testing Bed.</p><p>Because tests are our security belt and writing readable test is as important as writing readable code. Tests consist of 3 parts, which are arrange, action, assert.</p><p>Arrange: Preparation of data or objects before execution of test in order to test specific behavior.</p><p>Action: Execution of specific behavior.</p><p>Assert: After execution of specific behavior, we expect something to happen and we check if it happens or not.</p><pre>// Arrange<br>whenever(user.status).thenReturn(HIDDEN) whenever(userDao.getUser(any())).thenReturn(user) </pre><pre>val userRepository = UserRepository(userDao,...)</pre><pre>// Act<br>val profile = userRepository.getUserProfile()</pre><pre><br>// Assert<br>assertThat(profile).isNull()</pre><h3>Robot Pattern:</h3><p>Robot Pattern is one of the most popular Android Instrumentation Unit Testing Pattern. We can easily say that it is defining our interaction in different class, whose name is Robot and making use of power of Builder Pattern.</p><pre>LoginRobot()// Arrange<br>.typeEmail(&quot;cs.ibrahimyilmaz@gmail.com&quot;)<br>.typePassword(&quot;donttry&quot;)<br>.clickLoginButton() // Act<br>.checkErrorMessage(&quot;wrong password&quot;) // Assert</pre><p>But as you can see that it is Ok but now well-structured as above.</p><p>In that point, Kotlin helps us with High-Order Functions.</p><pre>fun loginRobot(<br>    block: LoginRobot.() -&gt; Unit<br>) = LoginRobot().block()</pre><p>Now we can implement our test like that:</p><pre>fun should_show_the_wrong_email_or_password_error_when_the_login_fails()=loginRobot{</pre><pre> // Arrange<br> typeEmail(&quot;cs.ibrahimyilmaz@gmail.com&quot;)<br> typePassword(&quot;donttry&quot;)<br> <br> // Act<br> clickLoginButton() <br> <br> // Assert checkErrorMessage(R.string.login__error_message_wrong_email_or_password)</pre><pre>}</pre><p>Enjoy brewing many unit tests!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a98db0c10b92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LiveData and IllegalStateException: FragmentManager is already executing transaction]]></title>
            <link>https://cs-ibrahimyilmaz.medium.com/livedata-and-illegalstateexception-fragmentmanager-is-already-executing-transaction-235a6330cc12?source=rss-bfa5f552d0f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/235a6330cc12</guid>
            <category><![CDATA[illegalstateexception]]></category>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[livedata]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[dialogfragment]]></category>
            <dc:creator><![CDATA[Ibrahim Yilmaz]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 22:02:57 GMT</pubDate>
            <atom:updated>2021-05-13T18:10:08.433Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wSfD4w1tBet-Vke-" /><figcaption>Photo by <a href="https://unsplash.com/@rojekilian?utm_source=medium&amp;utm_medium=referral">Sarah Kilian</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello Android Fellas,</p><p>Recently I have faced with this crash :</p><blockquote>`IllegalStateException: FragmentManager is already executing transaction`.</blockquote><p>It seemed to be strange because all of unit tests in the pipeline passes successfully and testers run smoke tests in Test and Production environments and no crash.</p><p>It is good or bad practice it is another topic the crash happened where we want to show Dialog by using DialogFragment.</p><p>If I summary flow between entities:</p><blockquote><strong>ViewModel</strong> -&gt; <strong>LiveData</strong> -&gt; <strong>Fragment</strong>-&gt; <strong>DialogFragment</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4484b4c6aae3f19f48ddc42147f8fc96/href">https://medium.com/media/4484b4c6aae3f19f48ddc42147f8fc96/href</a></iframe><p>Source code more or less something like that and the main problem is that it does not crash everytime:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b87c2f86caeecfa6afdba12ce6f6336f/href">https://medium.com/media/b87c2f86caeecfa6afdba12ce6f6336f/href</a></iframe><p>In order to reproduce the crash, I tried to use showNow()</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/303e12de8851d0717cc2c1d445e0a1a1/href">https://medium.com/media/303e12de8851d0717cc2c1d445e0a1a1/href</a></iframe><p>Still same crash happens sometimes but more than the previous one.</p><p>Problem is still not visible. But now we have more IllegalStatementException. The solution was to revert code back to previous one and have that crash less than latest version or try to dig it more.</p><p>When we check the crash log. It seems that this crash happens right after the fragment is created. It seems that it is impossible to have that because showError happens after some long running task and this should not happen. Because when Fragment is created, the value of LiveData&lt;Boolean&gt; is null and it should not fire any event.</p><p>It seems that something magical happens in the life cycle and resource management.</p><p>I just set the initial data for LiveData in ViewModel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/358e5f7883d6ecbba2a8809bededd96c/href">https://medium.com/media/358e5f7883d6ecbba2a8809bededd96c/href</a></iframe><p>And hurra! I start to reproduce the crash!</p><p>Reason: It seems that Android somehow kills and creates the caller Fragment. But we have ViewModel and LiveData. Observer directly called right after Fragment called. While its transaction is ongoing, it tries to execute new transaction in order to show DialogFragment.</p><p>Solution: Instead of using fragmentManager(because it is activity’s supportFragmentManager), we should use <strong>childFragmentManager</strong>. In that way Android can manage to handle Fragment Transactions. The crash doesn’t happen again.</p><p>I hope this could help you.</p><p>Have fun.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=235a6330cc12" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>