<?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 Mohit Sharma on Medium]]></title>
        <description><![CDATA[Stories by Mohit Sharma on Medium]]></description>
        <link>https://medium.com/@mohitsharma_49363?source=rss-561316ca2f2d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BQ0uDLN5M6ds2EUIs3r5dA.jpeg</url>
            <title>Stories by Mohit Sharma on Medium</title>
            <link>https://medium.com/@mohitsharma_49363?source=rss-561316ca2f2d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 14:33:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohitsharma_49363/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[Android ViewModel with Custom Arguments]]></title>
            <link>https://medium.com/android-news/android-viewmodel-with-custom-arguments-d0ff0fba29e1?source=rss-561316ca2f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0ff0fba29e1</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mohit Sharma]]></dc:creator>
            <pubDate>Thu, 21 Dec 2017 22:09:29 GMT</pubDate>
            <atom:updated>2017-12-27T18:53:47.830Z</atom:updated>
            <content:encoded><![CDATA[<p>Android Arch component has 2 types of view model. One is <a href="https://developer.android.com/reference/android/arch/lifecycle/ViewModel.html">ViewModel</a> and <a href="https://developer.android.com/reference/android/arch/lifecycle/AndroidViewModel.html">AndroidViewModel</a>, differnce between those 2 is first one does not take any argument and latter one take application object as argument. Most of time we want to pass our custom argument to viewmodel and we can do that by extending <a href="https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.html">ViewModelProvider.NewInstanceFactory</a>() and in which we have to override the create method. Below is the example how we can create our CustomViewModelFactory</p><pre>class CustomViewModelFactory(private val <strong>test</strong>: String) : ViewModelProvider.NewInstanceFactory() {<br><br>    override fun &lt;T : ViewModel?&gt; create(modelClass: Class&lt;T&gt;?): T {<br>        return LoginViewmodel(<strong>test</strong>) as T<br>    }<br><br>}</pre><p>In the activity class, we can create our view model with custom argument something like this</p><pre>val viewModel = ViewModelProviders.of<br>(this,CustomViewModelFactory(<strong>&quot;somerandomvalue&quot;</strong>)).get(LoginViewmodel::class.<em>java</em>)</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0ff0fba29e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/android-viewmodel-with-custom-arguments-d0ff0fba29e1">Android ViewModel with Custom Arguments</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Detect App Foreground Time]]></title>
            <link>https://medium.com/@mohitsharma_49363/android-detect-app-foreground-time-9b4f6752b077?source=rss-561316ca2f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b4f6752b077</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Mohit Sharma]]></dc:creator>
            <pubDate>Wed, 07 Jun 2017 22:14:09 GMT</pubDate>
            <atom:updated>2017-06-07T22:14:09.812Z</atom:updated>
            <content:encoded><![CDATA[<p>I was working on one of a project where I want to determine the app foreground time for which user was using the app. Surprisingly Android OS does not provide any easy way to determine when an app is going in the background, Basically, we can divide the problem into multiple parts first detect when the app is launched and when an app is going in the background. I end up using registerActivityLifecycleCallbacks. I used ActivityLifecycleCallbacks interface which allows us to override below methods.</p><pre>public interface ActivityLifecycleCallbacks {<br>    void onActivityCreated(Activity activity, Bundle      savedInstanceState);<br>    void onActivityStarted(Activity activity);<br>    void onActivityResumed(Activity activity);<br>    void onActivityPaused(Activity activity);<br>    void onActivityStopped(Activity activity);<br>    void onActivitySaveInstanceState(Activity activity, Bundle outState);<br>    void onActivityDestroyed(Activity activity);<br>}</pre><p>Once we register for this ActivityLifecycleCallbacks anytime in our application in any activity any lifecycle event happens this method are also called. In onResume we check number of activities if its zero user is launching the app and set the start time and similarly when onStop is called if number of activities is zero user is leaving the app. Below is sample code which i used to detect app foreground time.</p><pre>class AppBackgroundHelper private constructor() : Application.ActivityLifecycleCallbacks {<br><br>    companion object {<br>        private val TAG = AppBackgroundHelper.<em>javaClass</em>.<em>name<br><br>        </em>private lateinit var singleton: AppBackgroundHelper<br><br>        fun init(application: Application) {<br>            if (singleton == null) {<br>                singleton = AppBackgroundHelper()<br>                application.registerActivityLifecycleCallbacks(singleton)<br>            } else {<br>                Log.v(TAG, &quot;AppBackgroundHelper already initialized&quot;)<br>            }<br>        }<br>    }<br><br>    private var numberOfActivities = 0<br>    private var startTime = 0L<br>    <br>    override fun onActivityPaused(activity: Activity?) {<br><br>    }<br><br>    override fun onActivityResumed(activity: Activity?) {<br>        if (numberOfActivities == 0) {<br>            startTime = System.currentTimeMillis()<br>        }<br>        numberOfActivities++<br>    }<br><br>    override fun onActivityStarted(activity: Activity?) {<br>    }<br><br>    override fun onActivityDestroyed(activity: Activity?) {<br>    }<br><br>    override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {<br>    }<br><br>    override fun onActivityStopped(activity: Activity?) {<br>        numberOfActivities--<br>        if (numberOfActivities == 0) {<br>            val appUsedTime = System.currentTimeMillis() - startTime<br>            val timeString = String.<em>format</em>(&quot;%02d:%02d:%02d&quot;, TimeUnit.MILLISECONDS.toHours(appUsedTime),<br>                    TimeUnit.MILLISECONDS.toMinutes(appUsedTime) % TimeUnit.HOURS.toMinutes(1),<br>                    TimeUnit.MILLISECONDS.toSeconds(appUsedTime) % TimeUnit.MINUTES.toSeconds(1))<br><br>            Log.v(TAG, &quot;User used app for $timeString&quot;)<br>        }<br>    }<br><br>    override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {<br>    }<br><br>}</pre><p>Please make sure to call init method from class which extends Application class.</p><pre>AppBackgroundHelper.init(this)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b4f6752b077" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android change touch area of View by TouchDelegate]]></title>
            <link>https://medium.com/android-news/android-change-touch-area-of-view-by-touchdelegate-fc19f2a34021?source=rss-561316ca2f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc19f2a34021</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Mohit Sharma]]></dc:creator>
            <pubDate>Thu, 25 May 2017 18:39:58 GMT</pubDate>
            <atom:updated>2017-09-18T07:51:51.047Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently while using one application I noticed that application has the pretty small buttons and sometimes it pretty tough to click those views. Ideally, all clickable view should be large enough in UI so that they are easily clickable but still if we have a small view on the screen which are pretty small we can increase the touch area of this views while keeping the size of those views same. I am gonna discuss a couple of different solution which we can use to increase the touchable area of view.</p><p>Let’s assume we want to increase the clickable area of the button which is defined in our layout</p><pre>&lt;Button<br> android:id=&quot;@+id/button&quot;<br> android:layout_width=&quot;wrap_content&quot;<br> android:layout_height=&quot;wrap_content&quot;<br> android:text=&quot;@string/click_me&quot; /&gt;</pre><p>(1) Put this view in another View container like frame layout and add padding to the amount you want to increase the touchable area of view. Then, set the click listener for view group in which we call the “performClick” method on our button. That will increase the touch area of the button. But if you observe right now when you click outside the bound of the button but inside the view group on click is called but the button is not showing it’s pressed state. We can enable that too by adding this attribute in our button<br>“android:duplicateParentState=”true””</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TQkeZSpGXVSbCR4QLscX1A.png" /><figcaption>Frame Layout with 20dp padding</figcaption></figure><p>(2) Use TouchDelegate: Android provide this class for doing this specific thing. To use this class, we should get current “hit rectangle” of the view. “Hit Rectangle” means the current touchable area of view and then we change the bound of that rectangle based on our need. In my case I am adding touchable are on all sides. Then creating the touch delegate object based on the new rectangle and pass the view whose touchable bound want to change. Then set the newly created touch delegate object to the parent of the view as touch delegate. <br>Below is the helper method which I created to change the touchable area of any view</p><pre>private fun changeTouchableAreaOfView(view: View, extraSpace: Int) {<br>    val parent = view.<em>parent </em>as View<br>    Observable.just(parent)<br>            .observeOn(AndroidSchedulers.mainThread())<br>            .subscribeOn(AndroidSchedulers.mainThread())<br>            .subscribe <strong>{<br>                </strong>val touchableArea = Rect()<br>                view.getHitRect(touchableArea)<br>                touchableArea.top -= extraSpace<br>                touchableArea.bottom += extraSpace<br>                touchableArea.left -= extraSpace<br>                touchableArea.right += extraSpace<br>                parent.<em>touchDelegate </em>= TouchDelegate(touchableArea, button)<br>            <strong>}<br><br>    </strong>/* In case you don&#39;t want to use Rx java<br>    parent.post {<br>        val touchableArea = Rect()<br>        button.getHitRect(touchableArea)<br>        touchableArea.top -= extraSpace<br>        touchableArea.bottom += extraSpace<br>        touchableArea.left -= extraSpace<br>        touchableArea.right += extraSpace<br>        parent.touchDelegate = TouchDelegate(touchableArea, button)<br>    }<br>    */<br><br>}</pre><p>For more info: <a href="https://developer.android.com/reference/android/view/TouchDelegate.html">https://developer.android.com/reference/android/view/TouchDelegate.html</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc19f2a34021" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/android-change-touch-area-of-view-by-touchdelegate-fc19f2a34021">Android change touch area of View by TouchDelegate</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin lazy, lateinit and Delegates]]></title>
            <link>https://medium.com/@mohitsharma_49363/android-kotlin-lazy-lateinit-and-delegates-9e5f01c561dc?source=rss-561316ca2f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e5f01c561dc</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[lazy]]></category>
            <dc:creator><![CDATA[Mohit Sharma]]></dc:creator>
            <pubDate>Tue, 23 May 2017 23:20:55 GMT</pubDate>
            <atom:updated>2017-05-24T01:12:22.055Z</atom:updated>
            <content:encoded><![CDATA[<p>In Kotlin when we define variable either we have to initialize with some value or define them in such way value can be null for those variables. In some cases, we want to initialize the value later but don’t want the variables to have the null value. For example: Suppose you want some variable which should be available at the class level and but will be initialized in the onCreate method. Kotlin provides following the following to implement something similar and based on need we can use one of them</p><p>(1) Lazy: It’s same as lazy initialization. Your variable will not be initialized unless you use that variable in your code. It will be initialized only once after that we always use the same value.</p><pre>val test: String by <em>lazy </em><strong>{<br>    </strong>val testString = &quot;some value&quot;<br>    testString<br><strong>}<br></strong>fun doSomething() {<br>    <em>println</em>(&quot;Length of string is &quot;+test.length)<br>}</pre><p>(2) lateinit: it means you will be initializing the value for this variable before accessing it. In case we try to access the variable before initializing it we will see this error “Caused by: kotlin.UninitializedPropertyAccessException: lateinit property test has not been initialized”. You cannot use lateinit for primitive type properties like Int, Long etc. For those, we can use delegates which will be explained in next section</p><pre>lateinit var test: String<br>fun doSomething() {<br>    test = &quot;Some value&quot;<br>    <em>println</em>(&quot;Length of string is &quot;+test.length)<br>    test = &quot;change value&quot;<br>}</pre><p>(3) Delegates.notNull&lt;Any&gt;: It works with primitive type properties also. notNull create an extra object for each property although it’s pretty small and but if you have too many properties it can impact the performance of your app. lateinit is cheaper that Delegates.notNull. So try to use lateinit as compared to Delegates.notNull.</p><pre>var test by Delegates.notNull&lt;String&gt;()<br>fun doSomething() {<br>    test = &quot;Some value&quot;<br>    <em>println</em>(&quot;Length of string is &quot;+test.length)<br>    test = &quot;change value&quot;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e5f01c561dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android New App Architecture Example]]></title>
            <link>https://medium.com/@mohitsharma_49363/android-new-app-architecture-example-afa341ae57e0?source=rss-561316ca2f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/afa341ae57e0</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Mohit Sharma]]></dc:creator>
            <pubDate>Tue, 23 May 2017 15:22:53 GMT</pubDate>
            <atom:updated>2017-05-23T16:28:45.620Z</atom:updated>
            <content:encoded><![CDATA[<p>In my last <a href="https://medium.com/@mohitsharma_49363/android-app-architecture-viewmodel-concept-af04da8822c1">article</a>, I discussed new architecture. In this article lets implement that new Pattern in the Sample App. It’s a pretty simple app that shows basic UI in which you can enter Github username and it will display the name of all your repos. Below is the screenshot of the UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*Gs0jUqEWEzTdsyw7LlC7jQ.png" /><figcaption>Show all repos name based on github username</figcaption></figure><p>You need Android Studio 3.0 for this sample. You can download it from this link:</p><p><a href="https://developer.android.com/studio/preview/index.html">Android Studio Preview | Android Developers</a></p><p><strong>Note*</strong> This is still in the canary channel. so instead of updating the Android Studio from canary channel please install a separate copy of this version.</p><p><strong>(1)</strong> Open build.gradle from app module and add the following Dependency.</p><pre>compile &quot;android.arch.lifecycle:runtime:1.0.0-alpha1&quot;<br>compile &quot;android.arch.lifecycle:extensions:1.0.0-alpha1&quot;<br>annotationProcessor &quot;android.arch.lifecycle:compiler:1.0.0-alpha1&quot;<br></pre><p><strong>(2)</strong> Also add the Kotlin in your build.gradle as we are going to write a sample code in Kotlin. Also added tons of other dependencies in my project for Retrofit, RxJava, Dagger etc.</p><p><strong>(3)</strong> Set up the network component using Dagger. As this article is not about dagger I am not going to explain much about the Dagger. you can find more information from this <a href="https://github.com/google/dagger">link</a>.</p><p><strong>(4)</strong> Created a model class named Repo that has “name” property, which we are using for the demo purposes and also set up service layer using <strong>Retrofit </strong>for which more info can be found <a href="http://square.github.io/retrofit/">here</a>.</p><p><strong>(5)</strong> Created a DataRepository class in repository package. It takes retrofit service as an argument in the constructor. Right now we only have Retrofit service in this module but based on the requirement we can also DAO which will help us to access the database or provide both. It’s pretty simple as userNameObservable will be called from ViewModel which will then send any user name entered in UI and based on that it will make the network request and send the results back.</p><pre>class DataRepository(private val repoService: RepoService) {<br>    companion object {<br>        private val TAG = DataRepository.<em>javaClass</em>.<em>name<br>    </em>}<br><br>    var repoResponse = PublishSubject.create&lt;List&lt;Repo&gt;&gt;()<br>    val userNameObservable = PublishSubject.create&lt;String&gt;()<br><br>    init {<br>        userNameObservable.subscribe <strong>{ </strong>userName <strong>-&gt;<br>            </strong>repoService.fetchUserRepos(userName, object : Observer&lt;List&lt;Repo&gt;&gt; {<br>                override fun onCompleted() {<br>                    Log.v(TAG, &quot;On complete called&quot;)<br>                }<br><br>                override fun onError(e: Throwable) {<br>                    Log.v(TAG, &quot;on error called &quot; + e.message)<br>                }<br><br>                override fun onNext(repoList: List&lt;Repo&gt;) {<br>                    repoResponse.onNext(repoList)<br>                }<br>            })<br><br>        <strong>}<br>    </strong>}<br>}</pre><p><strong>Note*</strong> Right now I am not handling error state we can send a different signal on error so that the view model can tell view to show an appropriate error in the UI.</p><p><strong>(6)</strong> RepoViewModel extends ViewModel class. This class should have all the business logic and make changes to data it got from Retrofit service. It also passes any interaction from UI to Repository which make the network request to get the info. Please try to makes any LiveData property private so that we don’t have to access to its setter value in Fragment/Activity.</p><pre>class RepoViewModel(dataRepository: DataRepository) : ViewModel() {<br>    private var repoResponse: MutableLiveData&lt;List&lt;Repo&gt;&gt; = MutableLiveData()<br>    val repoObservable = PublishSubject.create&lt;String&gt;()<br><br>    init {<br>        repoObservable.subscribe(dataRepository.userNameObservable)<br>        dataRepository.repoResponse.subscribe <strong>{<br>            </strong>repoResponse.<em>value </em>= <strong>it<br>        }<br>    </strong>}<br><br>    fun getRepos(): MutableLiveData&lt;List&lt;Repo&gt;&gt; {<br>        return repoResponse<br>    }<br><br>}</pre><p><strong>(7)</strong> MainActivity class will inject the repository module which will be passed to view model so that it can pass any data request to repository module where we will handle data request.</p><pre>class MainActivity : LifecycleActivity() {<br>    @Inject<br>    lateinit var dataRepository: DataRepository<br><br>    private val repoViewModel: RepoViewModel by <em>lazy </em><strong>{<br>        </strong>RepoViewModel(dataRepository)<br>    <strong>}<br><br>    </strong>private lateinit var button: Button<br>    private lateinit var repoNameTextView: TextView<br>    private lateinit var userName: EditText<br><br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_main</em>)<br>        (<em>applicationContext </em>as MyApplication).repositoryComponent.inject(this)<br>        button = findViewById(R.id.<em>button</em>) as Button<br>        repoNameTextView = findViewById(R.id.<em>repo_names</em>) as TextView<br>        userName = findViewById(R.id.<em>user_name</em>) as EditText<br><br>        repoViewModel.getRepos().observe(this, <em>Observer </em><strong>{ </strong>repoList <strong>-&gt;<br>            </strong>var repoNames = &quot;&quot;<br>            repoList?.<em>forEach </em><strong>{ </strong>repo <strong>-&gt;<br>                </strong>repoNames = repoNames.plus(repo.name).plus(&quot;,&quot;)<br>            <strong>}<br>            </strong>repoNameTextView.<em>text </em>= &quot;Names of all repos  ${repoNames}&quot;<br>        <strong>}</strong>)<br><br>        button.setOnClickListener <strong>{<br>            </strong>repoViewModel.repoObservable.onNext(userName.<em>text</em>.toString())<br>        <strong>}<br><br><br>    </strong>}<br>}</pre><p>You can find the sample code for this app <a href="https://github.com/Mohit2705/BlogProjects">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afa341ae57e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android App Architecture ViewModel Concept]]></title>
            <link>https://medium.com/@mohitsharma_49363/android-app-architecture-viewmodel-concept-af04da8822c1?source=rss-561316ca2f2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/af04da8822c1</guid>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[architecture]]></category>
            <dc:creator><![CDATA[Mohit Sharma]]></dc:creator>
            <pubDate>Mon, 22 May 2017 04:27:05 GMT</pubDate>
            <atom:updated>2017-05-23T15:32:53.969Z</atom:updated>
            <content:encoded><![CDATA[<p>In Google IO 2017, Google recommended the App Architecture that can be used to make app more stable and robust. The application architecture can be divided into different parts, explained below.</p><p><strong>(1) Activity/ Fragment or Views:</strong> This component will show UI of the app that user sees and also pass any event happened on the UI to ViewModel which will react for those events. One thing to remember is, Our UI component should never directly talk to Repository. They should always pass event or get the data from view model which will be shown in the UI.</p><p><strong>(2) ViewModel:</strong> This component should have all the business logic and also provide data back to the View. One important thing about the view model is, it will survive the config changes too. So in case of config change happen, the activity will be recreated but view model that is created first time will be used which is explained in below diagram, taken from the Android documentation.</p><p><strong>NOTE* Another thing to make sure we should never pass context to view model.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*3Kr2-5HE0TLZ4eqq8UQCkQ.png" /><figcaption>ViewModel Lifecycle</figcaption></figure><p><strong>(3) Repository:</strong> This module handles data operations. This module will decide from where we should get data from network or local db. It abstract the data source.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*-yY0l4XD3kLcZz0rO1sfRA.png" /><figcaption>App Architecture</figcaption></figure><p>One important thing to remember is each component should talk to component which is above or below only. Our view should never talk to Repository. Same way View Model should never talk to Web service or Data base.</p><p>You can find sample code in this <a href="https://medium.com/@mohitsharma_49363/android-new-app-architecture-example-afa341ae57e0">article</a> in which i have implemented the above pattern.</p><p>For More Info: <a href="https://developer.android.com/topic/libraries/architecture/guide.html">https://developer.android.com/topic/libraries/architecture/guide.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af04da8822c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>