<?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 Mikhail Zhalskiy on Medium]]></title>
        <description><![CDATA[Stories by Mikhail Zhalskiy on Medium]]></description>
        <link>https://medium.com/@mikhail_zhalskiy?source=rss-7cf5d5764df8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PaEwUI5ZtaymgZFgX6TkPg.jpeg</url>
            <title>Stories by Mikhail Zhalskiy on Medium</title>
            <link>https://medium.com/@mikhail_zhalskiy?source=rss-7cf5d5764df8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 17:40:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mikhail_zhalskiy/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[Jetpack Compose Navigation and Dagger 2 with lazy initialization of modules.]]></title>
            <link>https://medium.com/@mikhail_zhalskiy/jetpack-compose-navigation-and-dagger-2-with-lazy-initialization-of-modules-782448cffaa1?source=rss-7cf5d5764df8------2</link>
            <guid isPermaLink="false">https://medium.com/p/782448cffaa1</guid>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[compose-navigation]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[multi-module-project]]></category>
            <dc:creator><![CDATA[Mikhail Zhalskiy]]></dc:creator>
            <pubDate>Thu, 13 Jul 2023 19:01:53 GMT</pubDate>
            <atom:updated>2023-07-13T19:01:53.811Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GXZv-WLd5XuCa1bu1i2eaA.jpeg" /></figure><p>Hi everyone! My name is Mikhail, I am Head of Android Division in Umbrella IT. The purpose of the article is to show how to combine Jetpack Compose Navigation and Dagger 2 with lazy initialization of modules.</p><blockquote>Before reading, you should read the article <a href="https://proandroiddev.com/modularization-of-android-applications-with-lazy-initialization-a091eaaa284a">Modularization of Android Applications with lazy initialization</a>.</blockquote><p>What goals do we want to achieve:</p><ul><li>UI feature-component is initialized at the moment when the feature screen is opened, if there are no other dependencies on this component</li><li>UI feature-component exists as long as the screen exists, if there are no other dependencies on this component</li></ul><p>Let’s make the project with the following modules and dependencies, implementing lazy initialization of components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d57x5jThByvQ68ozuoK0_Q.png" /></figure><h3>Let’s figure out how to initialize the component after navigating to the screen.</h3><p>Goals:</p><ul><li>Inject FeatureStarter into the calling feature as a dependency</li><li>Initialize component with <strong>FeatureStarter</strong>, only after navigation to screen</li></ul><p>All features UI provide <strong>FeatureStarter</strong> via their api</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0485094fea3df12b308853673a03990c/href">https://medium.com/media/0485094fea3df12b308853673a03990c/href</a></iframe><p>In the app module, we will create a <strong>NavigationComponent</strong> (Dagger component), which will be responsible for providing various <strong>FeatureStarters</strong> in features. Each feature will have its own <strong>FeatureStarterProxy</strong> that implements the corresponding <strong>FeatureStarter</strong>. Such proxying will allow you to provide a <strong>FeatureStarter</strong> without initializing a component of this feature until you navigate to its screen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f262c0f140a07e6ced27847702ca921b/href">https://medium.com/media/f262c0f140a07e6ced27847702ca921b/href</a></iframe><p>Let’s change dependencies of the component: now instead of <strong>FeatureApi</strong> we will pass <strong>NavigationApi</strong> as component dependency. It is still possible to get from it required <strong>FeatureStarter</strong>, which is actually a <strong>proxy</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7298958060972adb7b4068df12892450/href">https://medium.com/media/7298958060972adb7b4068df12892450/href</a></iframe><blockquote>Note: You can’t replace FeatureApi with NavigationApi if the feature component requires something, except FeatureStarter (e.g. interactor or repository)</blockquote><p><strong>NavigationApi</strong> should be bound to <strong>AppComponentDependencyHolder</strong>. <strong>NavigationApi</strong> exists as long as <strong>Application</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebf766e9001289f808476bbc94eb0819/href">https://medium.com/media/ebf766e9001289f808476bbc94eb0819/href</a></iframe><p>AppComponent bind in Application</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/452156e756740743034c8186c023d6d2/href">https://medium.com/media/452156e756740743034c8186c023d6d2/href</a></iframe><p>Let’s create the navigation graphs App and Detail.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41f639d8341f2ff61decf5254b02d3d9/href">https://medium.com/media/41f639d8341f2ff61decf5254b02d3d9/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b5b9308d57d0d9ce5530a20a660460e/href">https://medium.com/media/3b5b9308d57d0d9ce5530a20a660460e/href</a></iframe><p>In the App graph, the <strong>ListComponent</strong> and <strong>DetailComponent</strong> are only initialized after navigating to their screens. In the Detail graph, the initialization of the <strong>PdfComponent</strong> will happen after navigating to the Pdf screen, although the <strong>PdfFeatureStarter</strong> has already been added to the <strong>DetailDependencies</strong>, but as mentioned above, in fact, it is just a <strong>proxy</strong>.</p><h3>Let’s figure out how to bind a component to the screen lifecycle.</h3><p>Goals:</p><ul><li>The component must survive a configuration change</li><li>The component should be destroyed when the screen is destroyed</li><li>The component must not be destroyed while the screen is in the back stack</li></ul><p>Let’s create a store that will exist as long as the Application exists. The key in this storage will be <strong>NavBackStackEntry.id</strong> and the value will be the <strong>LifecycleEventObserver</strong> implementation containing <strong>BaseApi</strong>. By monitoring the <strong>Lifecycle.Event.ON_DESTROY</strong> message from the <strong>NavBackStackEntry</strong>, we will remove the <strong>BaseApi</strong> attached to the screen from the storage.</p><p>However, <strong>Lifecycle.Event.ON_DESTROY</strong> can fire on a config change, using <strong>NavBackStackEntry</strong> there is no way to tell if it’s a screen navigation or a config change. So let’s use <strong>Application.ActivityLifecycleCallback</strong>. When <strong>onActivityPreDestroyed</strong> is called, if a configuration change occurs - we’ll block the possibility of deleting <strong>BaseApi</strong> from the storage, and when <strong>onActivityDestroyed</strong> is called - we’ll unblock it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/902844ab3846e59bb7e431b68905dc53/href">https://medium.com/media/902844ab3846e59bb7e431b68905dc53/href</a></iframe><p>Register <strong>ActivityLifecycleCallbacks</strong> in application</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7cb5dbe2884c585eaaa6bd2e2694699/href">https://medium.com/media/c7cb5dbe2884c585eaaa6bd2e2694699/href</a></iframe><p>Let’s place the implementation of this class in the <strong>injector_compose</strong> module. Let’s create a component that returns an <strong>InjectorComposeApi</strong> and bind it to the <strong>AppComponentDependencyHolder</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18478b06884e7b439bc101a2fc101559/href">https://medium.com/media/18478b06884e7b439bc101a2fc101559/href</a></iframe><p>In the UI feature, we will add a dependency on <strong>BinderApiToEntryLifecycle</strong> and oblige <strong>FeatureApi</strong> to implement the <strong>BinderBaseApiToLifecycle</strong> interface, which will allow us to bind <strong>FeatureApi</strong> when opening our screen. Hide the binding logic in the <strong>Composable</strong> function <strong>BindApiToEntryLifecycle</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c71aac678db2a42d30d0f463421b55fb/href">https://medium.com/media/c71aac678db2a42d30d0f463421b55fb/href</a></iframe><p>It turns out the following logic. We create a navigation graph, at each point of the graph there is a <strong>NavBackStackEntry</strong>, we pass it to the <strong>FeatureStarter</strong>. Next, the wrapper function above the screen is called. In this function, using <strong>BindApiToEntryLifecycle</strong>, we’ll bind <strong>FeatureApi</strong> and <strong>NavBackStackEntry.id</strong>, and when <strong>NavBackStackEntry</strong> is destroyed, we’ll unbind <strong>FeatureApi</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e7845f2de4d947214a871f9fca04c5d/href">https://medium.com/media/0e7845f2de4d947214a871f9fca04c5d/href</a></iframe><p><a href="https://medium.com/@mikhail_zhalskiy/injecting-savedstatehandle-into-viewmodel-using-dagger-2-and-jetpack-compose-30b34df9ffd1">The previous article</a> showed how to inject a SavedStateHandle containing screen arguments into a ViewModel using Dagger2. The created <strong>Inject</strong> function is the point of injection of all the necessary dependencies for the ViewModel and can also be used in the approach described here.</p><p>Thanks for reading.</p><p>All code from article could be reached out <a href="https://github.com/MikhailZhalskiy/DaggerLazyMultiModule">here</a></p><p>Umbrella IT is a globally oriented company that has been providing web and mobile development and IT consulting services since 2009.<br>- 350+ tech experts;<br>- 350+ successful projects (Hamleys, Kardex, Variety, Rolling Stone, 9GAG).<br>- TOP-100 best outsourcing company according to IAOP Global Outsourcing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=782448cffaa1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Injecting SavedStateHandle into ViewModel using Dagger 2 and Jetpack Compose]]></title>
            <link>https://medium.com/@mikhail_zhalskiy/injecting-savedstatehandle-into-viewmodel-using-dagger-2-and-jetpack-compose-30b34df9ffd1?source=rss-7cf5d5764df8------2</link>
            <guid isPermaLink="false">https://medium.com/p/30b34df9ffd1</guid>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[savedstatehandle]]></category>
            <category><![CDATA[viewmodel]]></category>
            <dc:creator><![CDATA[Mikhail Zhalskiy]]></dc:creator>
            <pubDate>Thu, 08 Dec 2022 13:30:10 GMT</pubDate>
            <atom:updated>2022-12-08T18:16:14.862Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ben2_tpFM0oscBsvsiRstA.png" /></figure><p>Hi everyone! My name is Mikhail, I am Head of Android Division in Umbrella IT. The purpose of the article is to show how to use Dagger 2 to inject a SavedStateHandle into a ViewModel that is used in a composable function. Injecting a SavedStateHandle into the ViewModel will provide access to screen arguments directly within the ViewModel.</p><h4>Part 1: Setup Dagger 2</h4><p>Let’s use the <strong>AssistedInjection</strong> Dagger 2 function. Let’s create a generic interface for the AssistedFactory</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de643d71e8daf062c8b659eeef092889/href">https://medium.com/media/de643d71e8daf062c8b659eeef092889/href</a></iframe><ul><li>ViewModel constructor will be annotated with <strong>@AssistedInject</strong></li><li>Let’s add a SavedStateHandle type field with the <strong>@Assisted</strong> annotation to the ViewModel constructor.</li><li>Let’s add an interface to the ViewModel that will allow us to inject an additional field. The factory interface inherits the <strong>ViewModelAssistedFactory</strong> interface.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09d1e4c791dc4ea35359d95ef8dd7188/href">https://medium.com/media/09d1e4c791dc4ea35359d95ef8dd7188/href</a></iframe><p>Let’s collect all our <strong>ViewModelAssistedFactory</strong> in one place using <strong>multibindings</strong>. Create an annotation for the key:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b83f73f5eccb34105a5e47dc65fb5540/href">https://medium.com/media/b83f73f5eccb34105a5e47dc65fb5540/href</a></iframe><p>Let’s declare a dagger’s module to describe all <strong>ViewModelAssistedFactory</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d347f594cc4f00c96f2631b373ff07f/href">https://medium.com/media/1d347f594cc4f00c96f2631b373ff07f/href</a></iframe><p>And we will create a factory that will only be responsible for initializing the ViewModel using the <strong>ViewModelAssistedFactory</strong>. This factory will implement the <strong>ViewModelFactory</strong> interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc321f3024cef287271201f15ecfc912/href">https://medium.com/media/cc321f3024cef287271201f15ecfc912/href</a></iframe><p>At the end of the Dagger setup, let’s create a common dagger’s module for <strong>DaggerViewModelAssistedFactory</strong>, which can be reused in a multi-module application, and add dagger’s component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a51fd7665830c24a982dec4dd9eaf16f/href">https://medium.com/media/a51fd7665830c24a982dec4dd9eaf16f/href</a></iframe><h4>Part 2: Inject <strong>into a</strong> composable function</h4><p>The <strong>lifecycle-viewmodel-compose</strong> library, starting in <strong>version 2.5.0</strong>, introduced the viewModel() API, which uses a lambda factory to instantiate a ViewModel instance without requiring the creation of a custom ViewModelProvider.Factory. Using this API, we will create a <strong>daggerViewModel()</strong> composite function to get the ViewModel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78d769434e5f7cb5bba73fb0ade415cd/href">https://medium.com/media/78d769434e5f7cb5bba73fb0ade415cd/href</a></iframe><p>The main thing in this function is the getViewModelFactory() method, which returns our factory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d0d03e72f6a78c40b50bf4d2d9ab864/href">https://medium.com/media/7d0d03e72f6a78c40b50bf4d2d9ab864/href</a></iframe><p>As you can see from the implementation, the <strong>CompositionLocal</strong> tool is used to get the factory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc144ed99b39ded954da78de3541d3a5/href">https://medium.com/media/fc144ed99b39ded954da78de3541d3a5/href</a></iframe><p>It remains only to inject our factory using the <strong>CompositionLocalProvider</strong> at the desired top of the composable graph. Let’s create an <strong>Inject</strong> function for this, which takes a <strong>ViewModelFactory</strong> as a parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d9dbaa893635f981549617eaab131fb/href">https://medium.com/media/0d9dbaa893635f981549617eaab131fb/href</a></iframe><p>The <strong>Inject</strong> function will inject a factory from a dagger’s component at any screen nesting stage.</p><ul><li>If the project is monolithic or an intermediate FlowScreen is used, then we inject the factory by wrapping the NavHost.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdd7c05d60ad99f629cb1c8d5c027b0e/href">https://medium.com/media/bdd7c05d60ad99f629cb1c8d5c027b0e/href</a></iframe><ul><li>If the project is multi-module or screens are described in different places, then you can inject in <strong>composable() {}</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02286351cfc04a36b4b5883d2c8b60aa/href">https://medium.com/media/02286351cfc04a36b4b5883d2c8b60aa/href</a></iframe><p>The screen function for the default ViewModel parameter calls the <strong>daggerViewModel()</strong> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83035a81ace59d6dd2946f5c5728a073/href">https://medium.com/media/83035a81ace59d6dd2946f5c5728a073/href</a></iframe><p>Thanks for reading.</p><p>All of the code written for this post is available <a href="https://github.com/MikhailZhalskiy/ComposeViewModelDagger">here</a></p><p>Umbrella IT is a globally oriented company that has been providing web and mobile development and IT consulting services since 2009.<br>- 350+ tech experts;<br>- 350+ successful projects (Hamleys, Kardex, Variety, Rolling Stone, 9GAG).<br>- TOP-100 best outsourcing company according to IAOP Global Outsourcing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30b34df9ffd1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>