Skip to main content

Posts

Showing posts with the label Navigation

Jetpack Compose: Navigation object argument

Jetpack Compose: Navigation with Object Arguments Jetpack Compose is a powerful toolkit for building native Android UIs with a declarative approach. It simplifies UI creation by leveraging Kotlin code instead of XML layouts. In this article, we will explore an interesting feature of Jetpack Compose: passing complex objects through navigation using Gson, focusing on how to pass an object between screens in a Compose-based application. Navigation in Jetpack Compose provides a simple way to handle navigation between different composables. However, unlike primitive types like strings or integers, passing more complex data such as objects requires a bit more setup. In this example, we'll see how to pass an object by serializing it into a JSON string and restoring it on the target screen using Gson, a popular JSON library in Android. MainActivity: Entry Point and Scaffold Setup The main entry point for the application is the MainActivity , which extends AppCompatActivity . Inside the onC...

Jetpack Compose: Navigation arguments data type

Introduction Jetpack Compose, Android's modern toolkit for building native UI, simplifies UI development by allowing declarative programming and removing the need for XML layouts. One key feature is the ability to implement navigation using the NavHost and NavController classes. This tutorial breaks down a Kotlin example demonstrating how to navigate between screens and pass arguments of different data types using Jetpack Compose. In this example, we will explore how to pass a string and a long data type as arguments during navigation between two composable screens. The first screen displays a list of color names, while the second screen, accessed by clicking on a color, shows the selected color with its hexadecimal value applied to the background. Main Structure The application begins in MainActivity , which hosts the app's composable functions using setContent . Instead of traditional XML layouts, we define the UI inside composable functions. The GetScaffold() function set...

Jetpack Compose: Navigation multiple arguments

Introduction Jetpack Compose has revolutionized the way developers build UI in Android by providing a declarative approach to UI design. With its integration of navigation components, handling multiple arguments across different screens becomes more streamlined and efficient. In this article, we explore how to pass multiple arguments between screens using Jetpack Compose’s navigation system in a simple yet practical example. The code walks through the process of setting up a navigation system that passes color names and their hexadecimal values from one screen to another, leveraging Jetpack Compose’s powerful toolkit. This tutorial breaks down the key components of the Android Kotlin code, starting from setting up the scaffold and navigation controller to defining composable screens that handle and display the passed arguments. By the end, you'll have a solid understanding of how to manage navigation with multiple arguments in Jetpack Compose, making your apps more dynamic and user...

Jetpack Compose: Navigate with argument

Introduction In modern Android app development, Jetpack Compose has emerged as a powerful and declarative UI framework, offering a fresh approach to building Android user interfaces. One of its most exciting features is seamless navigation between different screens within an app, including the ability to pass data, or arguments, between these screens. This capability is particularly useful for creating dynamic and interactive applications. In this article, we will explore a practical example of using Jetpack Compose to navigate between screens with arguments, written in Kotlin. The code demonstrates how to manage navigation from a list of color names to a detailed view of each color, highlighting the simplicity and flexibility of Jetpack Compose's navigation system. This tutorial will break down the Kotlin code example step by step, explaining how navigation works in a Compose app, how to pass arguments between composables, and how to structure your app’s UI efficiently using Jetpa...

Jetpack compose: How to use navigation controller

Introduction This code demonstrates how to implement navigation within a Jetpack Compose application using the Navigation Compose library. It showcases a simple app with three color-coded screens ("Red", "Green", and "Blue") that users can navigate between. Breakdown The code is structured within the MainActivity.kt file. Here's a breakdown of the key components: NavController: This object, created using rememberNavController() , is the central point for navigation control. It keeps track of the back stack (history of visited screens) and facilitates navigation actions. Scaffold: This composable provides a pre-built layout structure with a TopAppBar and content area. It's used here to organize the app's overall UI. MainContent & MainNavigation: These composables handle the central content area. MainContent uses a Box with centered alignment to position the navigation component, MainNavigation . NavHost: This composable ...