Skip to main content

Posts

Showing posts from 2022

ViewModel with Jetpack Compose

  Compose uses remember API to store object in memory. Stored value is returned during recomposition . remember helps us retain data across recompostion , but when configuration changes happen all stored values are lost . One way to overcome this is to use rememberSaveable . rememberSaveable saves any value that can be saved in a Bundle , so it will survive configuration changes.  But when we are using lot of data , for example a list we can cannot use a rememberSavble beacuse there is limit on amount of data that can be stored in Bundle . So we use ViweModel . ViewModel provide the ui state and access to the business logic located in other layers of the app. It also survives configuration changes. ViewModel handles events coming from the UI or other layers of the app and updates the state holder based on the events. We need to add the following dependency in our app level build.gradle to use ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1" F...

Jetpack Compose with remember and mutableStateOf

  Jetpack Compose is a modern declarative UI Toolkit . In jetpack's declarative approach widgets are stateless and does not expose getter or setter function . So we cannot update UI with button.setText(String) or img.setImageBitmap()....  In Compose we build UI by defining set of Composable function. Composable function take in data and emit UI elements. So in Compose  only way to  update the UI is by calling the same Composable function with new Arguments. This Argument represents UI State. State in an app is any value that changes over time , this includes simple class variable to Room Database.  Any time a State is updated we need to call the Composable with new State to update the UI. This is called ReComposition .  This Demo Jetpack Compose app will help you understand the basics of Jetpack Compose. Screenshots of this App   This is our MainActivity package com.arun.androidtutsforu.democompose import android.os.Bundle import androidx.activit...

Fragments In Android Kotlin

Fragments Represent Reusable portion of your apps UI  Fragments are used to define and manage UI of a single screen or portion of screen. Fragments has it's own layout , Life cycle and can handle input events Fragments can not exist on its own it must be hosted by an Activity or another Fragment Activities act as a frame that contains the fragment .Each fragment operates like a view in the activity but it has  has it's own layout , Life cycle and can handle input events. Activities are ideal place to put global elements around your app's UI like Navigation Drawer, Toolbar , Bottom Navigation and fragments are better suited to define UI of single screen or portion of screen. You can use multiple Fragments with single Activity ,  this gives more modularity and reusability to our code. Add the following dependency in your app level build.gradle file to use fragments implementation "androidx.fragment:fragment-ktx:1.3.6" Now create a new fragment  package com.arun.and...