<?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 Dmytro Saviuk on Medium]]></title>
        <description><![CDATA[Stories by Dmytro Saviuk on Medium]]></description>
        <link>https://medium.com/@dimezis?source=rss-6890c56acd30------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lpVitgtQBT3ynuZ4dFkvwQ.jpeg</url>
            <title>Stories by Dmytro Saviuk on Medium</title>
            <link>https://medium.com/@dimezis?source=rss-6890c56acd30------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 17:57:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dimezis/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[How Android 8+  lets you get away with touching Views from any thread]]></title>
            <link>https://dimezis.medium.com/how-android-8-lets-you-get-away-with-touching-views-from-any-thread-906419b368fa?source=rss-6890c56acd30------2</link>
            <guid isPermaLink="false">https://medium.com/p/906419b368fa</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Dmytro Saviuk]]></dc:creator>
            <pubDate>Thu, 28 Mar 2019 22:32:49 GMT</pubDate>
            <atom:updated>2019-03-30T15:40:15.540Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Android 8+ lets you get away with touching Views from any thread</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VxcTm2frqyT2zL6ttGK6Jw.jpeg" /></figure><p>Android 8 has introduced an interesting optimization, which presumably allows a faster invalidation process of the Views, but has a harmful side effect. Let’s review the old and new flow and see what issues the new one has.</p><p>When a View invalidates itself, it talks to its ViewParent by calling invalidateChild. The ViewGroup’s implementation of this method looks like this on Android 8/9:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/702026d2f1e0bbce3eb94a15a4aa4b59/href">https://medium.com/media/702026d2f1e0bbce3eb94a15a4aa4b59/href</a></iframe><p>Now let’s start from the second “slow” branch. Prior to Android 8, it was the only path in this method. In Android 8+ it’s generally not used, I guess because most of the time you’ll be able to go through fast hardware accelerated path.</p><p>Useless details aside, this invalidateChildInParent method is called recursively until it reaches the dreadful ViewRootImpl, the top parent of the View hierarchy.</p><p>And this is the exact place where Android does check if you tried to “touch” the View from a non-UI thread. So normally, if you’re trying to call any method that triggers invalidate (e.g. setTextColor, setText) from other thread, you end up here crashing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1ba7c52095846c1bcae615419b2e2dd/href">https://medium.com/media/d1ba7c52095846c1bcae615419b2e2dd/href</a></iframe><p>Let’s take a look at the new fast path invalidation branch:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a863dd4397d8d1f5099a40c76aad5df8/href">https://medium.com/media/a863dd4397d8d1f5099a40c76aad5df8/href</a></iframe><p>Similarly to previous flow, the View notifies the parent about invalidation recursively, but this time by calling the onDescendantInvalidated method. As before, it goes up to ViewRootImpl, where this stuff gets executed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe686d43f539bf62737e9023cd7def0c/href">https://medium.com/media/fe686d43f539bf62737e9023cd7def0c/href</a></iframe><p>And as you can see, there are no thread checks whatsoever. Not here, not further in code.</p><p>So following the fast path, we can do virtually any View manipulation from any thread (except things that are triggering requestLayout, because it always goes through safe checks).<br>Not only it won’t crash, but it will actually work and render your changes.</p><p>Try it out, just don’t forget to set a fixed size to your TextView, otherwise it will try to resize the TextView on text change and crash on requestLayout.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f1cc85462a172bf533b3f0c49451de6/href">https://medium.com/media/5f1cc85462a172bf533b3f0c49451de6/href</a></iframe><p>Why is it a problem? Well, Android UI toolkit is not thread-safe, i.e. it’s not designed to support interactions from different threads. It means that by doing so, you may have race conditions, inconsistent View states, rendering issues and who knows what else.<br>Apart from that, on Android 8+ you might just miss a crash that would take place on earlier versions.</p><p>I raised a corresponding <a href="https://issuetracker.google.com/issues/129362490">issue</a> on Google tracker, so hopefully, this will get enough attention.</p><p>Meanwhile, be aware and don’t rely on this behavior.</p><p>Update:</p><p>Yaay, <a href="https://android-review.googlesource.com/c/platform/frameworks/base/+/937477">it’s fixed</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=906419b368fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android scroll position restoring done right]]></title>
            <link>https://dimezis.medium.com/android-scroll-position-restoring-done-right-cff1e2104ac7?source=rss-6890c56acd30------2</link>
            <guid isPermaLink="false">https://medium.com/p/cff1e2104ac7</guid>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Dmytro Saviuk]]></dc:creator>
            <pubDate>Tue, 24 Jan 2017 13:55:26 GMT</pubDate>
            <atom:updated>2018-10-04T10:23:38.401Z</atom:updated>
            <content:encoded><![CDATA[<p>For some reason there are a lot of quite misleading tips/suggestions on how to save and restore scroll position in your_scrolling_container upon orientation changes.</p><ul><li>Taking current scroll position and saving it in Activity’s onSaveInstanceState</li><li>Extending a certain scrollable View to do same there</li><li>Preventing Activity from being destroyed on rotation</li></ul><p>And yeah, they are working fine, but…</p><p>But in fact, everything is much simpler, because Android is already doing it for you!</p><p>If you take a closer look at<br>RecyclerView/ListView/ScrollView/NestedScrollView sources, you’ll see that each of them is saving its scroll position in onSaveInstanceState. And during the first layout pass they are trying to scroll to this position in onLayout method.</p><p>There are only 2 things you need to do, to make sure it’s gonna work fine:</p><ol><li>Set an id for your scrollable view, which is probably already done. Otherwise Android won’t be able to save View state automatically.</li><li>Provide a data <strong>before </strong>the first layout pass, to have the same scroll boundaries you had before rotation. That’s the step where developers usually have some issues.</li></ol><p>So if you are loading your data asynchronously on each screen rotation and then posting it to the Main thread through the Looper queue, you will always end up with losing the previous scroll position. In fact, any</p><p>handler.post(this::setListItems);</p><p>will break it as well, because this action will be posted into the queue and delivered <strong>after</strong> the list has already started its layout.</p><p>The right way is to load data once, cache it, and set it in a blocking manner on rotation, without any handler interaction.</p><p>It requires your cache to survive orientation changes, of course. It means that you need to keep it in something, that lives longer than Activity (ViewModels, Loaders, Retained Fragments, lastCustomNonConfigurationInstance, Application, etc).</p><p>By the way, you may like Loaders or not, but you probably noticed, that a long time ago, when you were using a CursorLoader (which is loading stuff asynchronously) to show some list data, you never had problems with preserving scroll position on rotation. And there’s no magic. The only thing it does, is checking for a cache and delivering it instantly in the Main thread.</p><p>But that’s completely different story, it’s up to you how to design a cache. <br>The main idea is that you need to deliver data properly to not bother with manual position preserving.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cff1e2104ac7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>