<?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 Adrián Castro Torres on Medium]]></title>
        <description><![CDATA[Stories by Adrián Castro Torres on Medium]]></description>
        <link>https://medium.com/@adrict9?source=rss-8cfc61acb72d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NymQOo5rcBIWUc2min1L1g.jpeg</url>
            <title>Stories by Adrián Castro Torres on Medium</title>
            <link>https://medium.com/@adrict9?source=rss-8cfc61acb72d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 07:48:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adrict9/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[Implement Jetpack Compose Navigation in Compose Multiplatform [2/2]]]></title>
            <link>https://medium.com/bestsecret-tech/implement-jetpack-compose-navigation-in-compose-multiplatform-79fae4713d90?source=rss-8cfc61acb72d------2</link>
            <guid isPermaLink="false">https://medium.com/p/79fae4713d90</guid>
            <category><![CDATA[navigation-component]]></category>
            <category><![CDATA[voyager]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[compose-multiplatform]]></category>
            <dc:creator><![CDATA[Adrián Castro Torres]]></dc:creator>
            <pubDate>Wed, 11 Dec 2024 10:43:49 GMT</pubDate>
            <atom:updated>2025-02-18T09:23:57.562Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2hiFQuPvciyzZHof" /><figcaption>Photo by <a href="https://unsplash.com/@kathymack?utm_source=medium&amp;utm_medium=referral">Katherine McCormack</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you missed the first part of this article series, click here to learn how to <a href="https://medium.com/bestsecret-tech/migrate-voyager-screenmodel-to-compose-multiplatform-viewmodel-e36edaf4ea68">implement Jetpack ViewModel in Compose Multiplatform</a>, you will need it before implementing the navigation part ;)</p><p>Jetpack Compose Navigation is a robust library that simplifies managing navigation in Compose-based projects. It is now possible to use this library in Compose Multiplatform projects.</p><p>Below, you’ll learn how to integrate and use it effectively.</p><h4>1. Add Required Dependencies</h4><p>First, add the necessary Jetpack Compose Navigation dependency to your project if you haven’t done that before. Use the latest version, which you can find <a href="https://mvnrepository.com/artifact/org.jetbrains.androidx.navigation/navigation-compose">here</a>.</p><p>In your commonMain.dependencies, include the following:</p><pre>commonMain.dependencies { <br>    implementation(&quot;org.jetbrains.androidx.navigation:navigation-compose:2.8.0-alpha02&quot;)<br>}</pre><h4>2. Define Your Navigation Graph</h4><p>A navigation graph defines all possible routes in your app. Start by creating a NavHost and specifying your composable destinations.</p><p>For example:</p><pre>import androidx.navigation.NavHost<br>import androidx.navigation.compose.composable<br>import androidx.navigation.compose.rememberNavController<br><br>@Composable<br>fun AppNavigation() {<br>    val navController = rememberNavController()<br>    NavHost(navController = navController, startDestination = &quot;home&quot;) {<br>        composable(&quot;home&quot;) { HomeScreen(navController) }<br>        composable(&quot;details&quot;) { DetailsScreen(navController) }<br>    }<br>}</pre><p>Here:</p><ul><li>startDestination specifies the default screen.</li><li>Each composable represents a screen in the app.</li></ul><h4>3. Create Composable Screens</h4><p>Define your screens as Composable functions. For instance:</p><pre>@Composable<br>fun HomeScreen(navController: NavController) {<br>    Column {<br>        Text(&quot;Welcome to the Home Screen&quot;)<br>        Button(onClick = { navController.navigate(&quot;details&quot;) }) {<br>            Text(&quot;Go to Details&quot;)<br>        }<br>    }<br>}</pre><pre>@Composable<br>fun DetailsScreen(navController: NavController) {<br>    Column {<br>        Text(&quot;Welcome to the Details Screen&quot;)<br>        Button(onClick = { navController.popBackStack() }) {<br>            Text(&quot;Go Back&quot;)<br>        }<br>    }<br>}</pre><p>In the HomeScreen, a button navigates to the DetailsScreen. In the DetailsScreen, a button allows the user to return to the previous screen.</p><h4>4. Handle Arguments in Navigation</h4><p>If you need to pass arguments between screens, you can define routes with placeholders and retrieve arguments in your destination Composable.</p><p>For example:</p><pre>import androidx.navigation.NavHost<br>import androidx.navigation.compose.composable<br>import androidx.navigation.compose.rememberNavController<br><br>@Composable<br>fun AppNavigation() {<br>    val navController = rememberNavController()<br><br>    // Defining route with argument<br>    NavHost(navController, startDestination = &quot;home&quot;) {<br>      composable(&quot;details/{id}&quot;) { backStackEntry -&gt;<br>        val id = backStackEntry.arguments?.getString(&quot;id&quot;)<br>        DetailsScreen(id)<br>    }<br>}</pre><pre>// Passing arguments<br>navController.navigate(&quot;details/randomId&quot;)</pre><p>In DetailsScreen:</p><pre>@Composable<br>fun DetailsScreen(id: String?) {<br>    Text(&quot;Details Screen with ID: $id&quot;)<br>}</pre><h4>5. Add Navigation to Your Root Composable</h4><p>Finally, use your AppNavigation function as the root Composable in your app:</p><pre>@Composable<br>fun MyApp() {<br>    MaterialTheme {<br>        AppNavigation()<br>    }<br>}</pre><h3>Conclusion</h3><p>With these steps, you’ve successfully implemented Jetpack Compose Navigation in a Compose Multiplatform project. This integration allows you to manage navigation across screens seamlessly and is easy to adapt for more complex navigation scenarios.</p><p>If you have any doubts, feel free to contact me on LinkedIn: <a href="https://www.linkedin.com/in/adrianct/">https://www.linkedin.com/in/adrianct/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2RgutNRtHkEEvpvl" /><figcaption>Photo by <a href="https://unsplash.com/@jamie_fenn?utm_source=medium&amp;utm_medium=referral">Jamie Fenn</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79fae4713d90" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bestsecret-tech/implement-jetpack-compose-navigation-in-compose-multiplatform-79fae4713d90">Implement Jetpack Compose Navigation in Compose Multiplatform [2/2]</a> was originally published in <a href="https://medium.com/bestsecret-tech">BESTSECRET Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implement Jetpack ViewModel in Compose Multiplatform [1/2]]]></title>
            <link>https://medium.com/bestsecret-tech/migrate-voyager-screenmodel-to-compose-multiplatform-viewmodel-e36edaf4ea68?source=rss-8cfc61acb72d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e36edaf4ea68</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[kotlin-multiplatform]]></category>
            <category><![CDATA[compose-multiplatform]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[viewmodel]]></category>
            <dc:creator><![CDATA[Adrián Castro Torres]]></dc:creator>
            <pubDate>Fri, 24 May 2024 14:58:27 GMT</pubDate>
            <atom:updated>2025-02-28T16:15:46.887Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/715/1*9y_tlpJ_8LxMVPxEmjU6NQ.png" /></figure><p>Jetpack ViewModel is a powerful tool widely used in Android development, and it can now be integrated into Compose Multiplatform projects.</p><p>Below, you’ll learn how to set it up and use it in your project:</p><h4>1. Add Required Dependencies</h4><p>Start by adding the necessary dependencies for Jetpack ViewModel. Make sure to use the latest version, which you can find <a href="https://mvnrepository.com/artifact/org.jetbrains.androidx.navigation/navigation-compose">here</a>.</p><p>In your commonMain.dependencies, include:</p><pre>commonMain.dependencies { <br>    implementation(&quot;org.jetbrains.androidx.lifecycle:lifecycle-viewmodel:2.8.4&quot;)<br>}</pre><h4>2. Define a Jetpack ViewModel</h4><p>To create a ViewModel with Jetpack, define your class like this:</p><pre>class RandomViewModel : ViewModel() { <br>  //Your awesome code<br>}</pre><h4>3. Instantiate the ViewModel in the UI</h4><p>When using the ViewModel in your UI, you can instantiate it with the viewModel function:</p><pre>val viewModel = viewModel { RandomViewModel() }</pre><h4>4. Use viewModelScope for Coroutines</h4><p>If your ViewModel requires launching coroutines, you can use viewModelScope to manage them, exactly as it is done on native Android. For example:</p><pre>viewModelScope.launch {<br>    yourFunction()<br>}</pre><h4>5. Handle UI State with Jetpack ViewModel</h4><p>If your ViewModel needs to manage UI state, you can leverage MutableStateFlow and StateFlow. Here’s how:</p><pre>class RandomViewModel : ViewModel() { <br>    private val _state: MutableStateFlow&lt;State&gt; = MutableStateFlow(State.Idle)<br>    val state: StateFlow&lt;State&gt; = _state<br><br>    fun loadSomething() {<br>        _state.value = State.Loading<br>     }<br>}</pre><p>Now, you can update the state using _state and expose it to the UI through the public state property.</p><h3>Conclusion</h3><p>With these simple 5 steps, you’ve successfully implemented Jetpack ViewModel in your Compose Multiplatform project, and are <strong>now ready to </strong><a href="https://medium.com/@adrict9/implement-jetpack-compose-navigation-in-compose-multiplatform-79fae4713d90"><strong>implement Jetpack Compose Navigation</strong></a> and have the full implementation. See you on the other side, happy coding!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*hki2GIuvWDqamcLm" /></figure><p>If you have any doubts, feel free to contact me on LinkedIn: <a href="https://www.linkedin.com/in/adrianct/">https://www.linkedin.com/in/adrianct/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e36edaf4ea68" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bestsecret-tech/migrate-voyager-screenmodel-to-compose-multiplatform-viewmodel-e36edaf4ea68">Implement Jetpack ViewModel in Compose Multiplatform [1/2]</a> was originally published in <a href="https://medium.com/bestsecret-tech">BESTSECRET Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>