<?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 Adam Simek on Medium]]></title>
        <description><![CDATA[Stories by Adam Simek on Medium]]></description>
        <link>https://medium.com/@simekadam?source=rss-dc97a830842e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LEK4I6HJYeivQp-QhohtcA.jpeg</url>
            <title>Stories by Adam Simek on Medium</title>
            <link>https://medium.com/@simekadam?source=rss-dc97a830842e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:50:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@simekadam/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[Do not kill your Presenters! An untold story brought to you back from 2014]]></title>
            <link>https://medium.com/@simekadam/do-not-kill-your-presenters-an-untold-story-brought-to-you-back-from-2014-67285e5c1884?source=rss-dc97a830842e------2</link>
            <guid isPermaLink="false">https://medium.com/p/67285e5c1884</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Adam Simek]]></dc:creator>
            <pubDate>Thu, 05 Sep 2019 14:19:17 GMT</pubDate>
            <atom:updated>2019-09-05T14:19:17.613Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I found this never published draft on my Medium account right now and chuckled. I have decided to post it now with a little preface.</em></p><p>I’m so glad that this is now a solved problem. Officially, by Google. And well documented. Architecture Components and <a href="https://developer.android.com/topic/libraries/architecture/viewmodel">ViewModel</a> in particular is exactly what I was asking for back then. As no similar solution existed (Not sure if Inloopx’s <a href="https://github.com/inloop/AndroidViewModel">AndroidViewModel</a> lib already existed at that time) or I at least wasn’t aware of it I was developing my own crappy version of it. That’s what’s missing in this draft, I wanted to show my implementation and ideas, but as a lazy person with very short attention span I never actually finished it.</p><p>Now why I went to my Medium account in the first place? I wanted to kick off a short musing on how line height is treated on Android. The question now is if I should even start with it as it might as well be obsolete in five years from now.</p><p>Anyways, here’s what I wrote down back then. Maybe, maybe it will make you chuckle as too.</p><p><em>Disclaimer. Please read this article only if you are really familiar with what </em><a href="https://www.google.se/search?q=model+view+presenter"><em>MVP</em></a><em> is. If you are not then please read </em><a href="http://antonioleiva.com/mvp-android/"><em>this</em></a><em>, </em><a href="https://medium.com/@artem_zin/m-model-from-mvc-mvp-in-android-flow-and-mortar-bd1e50c45395"><em>this</em></a><em>, or </em><a href="http://magenic.com/Blog/Post/6/An-MVP-Pattern-for-Android"><em>this</em></a><em> before. This not a howto for implementation of MVP on Android but rather a rant on all what have I read about it before.</em></p><p><a href="https://www.google.se/search?q=model+view+presenter"><strong>MVP</strong></a><strong> really is a thing on Android lately.</strong> There is a lot of blogposts describing how you should change you code from just being a bunch of Activities and Fragments filled with a spaghetti (damn, I’m hungry), to some state of the art architecture with mutual coupling pushed somewhere down to Earths core and where tests nearly write itself. Every Android tech conference these days should have at least one talk about MVP, otherwise people would never know, that they can easily stop to worry about the lifecycle of Views, Fragments and Activities.<br>And this initiative is a good thing, I like it. Moving your bussines logic from Android specific components somewhere else is a good idea and people should be told to follow it. <br>Why I am writing this bullshit-ish nonsense then?</p><h2>There is something weird with with all these examples.</h2><p>Because I also jumped on the MVP train and started to try MVP in apps I build. But then, suddenly, I realized, that there is something wrong with all these howtos and examples. You ask what? The answer is dead simple. Presenters lives in views. Why is that a problem? Let me ask once again. <br><strong>Presenters lives in views.</strong> Why is that a problem on Android? You might get it. The answer stands upon the fact how MVP’s views are represented on Android. No matter whether you use Activities, Fragments, custom Views, or combination of previous as the implementation of your view layer, <strong>its all still affected by that weird lifecycle</strong>. Rotate you device and your Activity is gone. Or better said all data in it is gone — including your Presenters and its state. Same for Fragments and custom views.</p><h2>MVP does not help you with Android lifecycle in any way.</h2><p>However MVP might help you to decouple your code, to make it cleaner, and easier to maintain, to increase its testability it (at least in the way you can find it on the Internet) apparently does not help you with the hassle of “Android lifecycle” in any way. And that’s a shame. At least for me.</p><h2>Let’s dig deeper.</h2><p>Enough of plain words, what about looking at some code? Imagine simple app with one Activity and one Presenter. The Activity serves as our View and it contains one EditText, Button and TextView. The app will be used to create a list of Persons. A Person does have a name, therefore the EditText there. So the user fill some text to the input, then clicks on the button and new Person will be added to a list. Later on the user could possibly submit whole list to some server or export it to a file. That’s not a part of our exercise though. We will stay focused only View and Presenter.</p><p>MVP tells us that we should probably move the logic of adding users to list to the Presenter. Let’s create our Presenter then.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64df966c6a5e14f262226876043d4d29/href">https://medium.com/media/64df966c6a5e14f262226876043d4d29/href</a></iframe><p>Nothing complicated right?</p><p>For now the view will be the main place of our interest. Below you can see how it could be implemented.</p><p>Because we want to separate the Presenter from the actual view implementation we will create an interface for our view. In this simple example it contains just a single method that sets the number of already added persons.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/664b9504d25d6b4a213dad57ee97b1a0/href">https://medium.com/media/664b9504d25d6b4a213dad57ee97b1a0/href</a></iframe><p>It lies upon the particular implementation of the interface how it will eventually displayed to the user. You know, this is the nice thing about MVC. Let’s see the implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14074d8980d1a431d4c49d4395f303db/href">https://medium.com/media/14074d8980d1a431d4c49d4395f303db/href</a></iframe><p>I think the code is pretty selfdocumenting so let me rather focus straight at <em>line 7</em>. It is the place where we store a reference to our presenter. That itself wouldn’t be necesarily bad, the bad thing is that it is the outer most scoped reference of the presenter. When our Activity dies the Presenter will get effectively killed too — as well as all Persons in it. Simply said after any configuration change we lose the state. D’oh!</p><p>Doesn’t Android provides developers with mechanisms for saving state exactly in these cases? It certainly does, but <strong>wasn’t it MVP what should have helped us with that?</strong> It was, but it apparently did not. But it really could. So what to do about this?</p><h2>Singleton to the rescue.</h2><p>No, not really. Sorry for this tabloid heading. Faith in making our Presenters static would mislead us onto a way of polluting the heap with everliving Presenter instances. Static is not our friend in this case. We need something better.</p><p><em>I guess this is where I wanted to talk about my take on this :D</em></p><h2>A broader View.</h2><p>One could argue that we should actually not keep any state in Presenters. That Presenters should serve only for formatting data from a Model and displaying them in a View and for reacting on user input. That all state should be preserved in a Model instead. Sure, sure, that’s true, but with that approach you only push the issue somewhere else. You won’t solve it.</p><p>It all stands upon the fact that the entry point to any logical section of an Android app — any single screen — is an Activity. And that it dies with any configuration change. As do all you Fragments, Views, Presenters, Models, ViewModels, or just everything non-static and everything you created from the Activity and obviously also all these subsequent components.</p><p>Imagine navigating between Presenters which would then took care of creating its Views, Models and other components needed to express its content. View could still benefit from the power of having different sets of resources for different configurations, while your application logic will be (to some extend) free from all the hassle. Don’t they have this on iOS?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67285e5c1884" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>