Amol Pawar

ViewModel and rememberSaveable

The Truth About ViewModel and rememberSavable: Configuration Changes vs Process Death

If you’ve built Android apps with Jetpack Compose, you’ve probably run into the question: Should I use ViewModel or rememberSaveable? Both help you keep state alive, but they work very differently depending on what’s happening to your app — like when the screen rotates or when the system kills your process.

This post will break down ViewModel and rememberSaveable, explain when to use each, and show real code examples so it finally clicks.

The Basics: Why State Preservation Matters

On Android, your app doesn’t always stay alive. Two big events affect your app’s state:

  1. Configuration changes — like screen rotations, language changes, or switching dark mode. The activity is destroyed and recreated, but the process usually stays alive.
  2. Process death — when Android kills your app’s process (e.g., to reclaim memory) and later restores it when the user comes back.

If you don’t handle these correctly, your users lose whatever they were doing. That’s where ViewModel and rememberSaveable come in.

remember: The Starting Point in Compose

At the simplest level, you use remember in Jetpack Compose to keep state alive across recompositions.

Kotlin
@Composable
fun CounterScreen() {
    var count by remember { mutableStateOf(0) }

    Button(onClick = { count++ }) {
        Text("Count: $count")
    }
}
  • Here, count won’t reset when Compose redraws the UI.
  • But if the device rotates (configuration change), the state is lost because remember only survives recompositions, not activity recreation.

That’s why we need more powerful tools.

rememberSaveable: Survives Configuration Changes and Process Death

rememberSaveable goes one step further. It automatically saves your state into a Bundle using Android’s saved instance state mechanism.

Kotlin
@Composable
fun CounterScreen() {
    var count by rememberSaveable { mutableStateOf(0) }

    Button(onClick = { count++ }) {
        Text("Count: $count")
    }
}

What happens here:

  • Rotate the screen? count survives.
  • App is killed and restored (process death)? count also survives, because it was written to the saved instance state.

Limitations:

  • Only works with data types that can be written to a Bundle (primitives, Strings, parcelables, etc.).
  • Not ideal for large objects or data fetched from a repository.

ViewModel: Survives Configuration Changes, Not Process Death

A ViewModel is a lifecycle-aware container designed to hold UI data. It’s tied to a LifecycleOwner like an activity or a navigation back stack entry.

Kotlin
class CounterViewModel : ViewModel() {
    var count by mutableStateOf(0)
}

@Composable
fun CounterScreen(viewModel: CounterViewModel = viewModel()) {
    Button(onClick = { viewModel.count++ }) {
        Text("Count: ${viewModel.count}")
    }
}

What happens here:

  • Rotate the screen? count survives. The same ViewModel instance is reused.
  • App is killed (process death)? count is lost. The ViewModel does not persist beyond process death.

Configuration Changes vs Process Death: Who Wins?

Here’s the clear breakdown:

When to Use rememberSaveable

Use rememberSaveable for small, lightweight UI state that:

  • Must survive both rotation and process death.
  • Can easily be serialized into a Bundle.

Examples:

  • Current tab index.
  • Form text fields.
  • Simple filter/sort options.

When to Use ViewModel

Use ViewModel for more complex or long-lived state that:

  • Doesn’t need to survive process death.
  • Might involve business logic, repositories, or data streams.
  • Should be scoped to the screen or navigation graph.

Examples:

  • Data loaded from a database or network.
  • Complex business logic.
  • State shared across multiple composables in the same screen.

Can You Combine Them? Yes.

Often, the best solution is to use ViewModel and rememberSaveable together.
 For example, a ViewModel manages your main UI state, but a few critical fields use rememberSaveable so they’re restored even after process death.

Kotlin
@Composable
fun FormScreen(viewModel: FormViewModel = viewModel()) {
    var userInput by rememberSaveable { mutableStateOf("") }

    Column {
        TextField(
            value = userInput,
            onValueChange = { userInput = it }
        )

        Button(onClick = { viewModel.submit(userInput) }) {
            Text("Submit")
        }
    }
}

Here:

  • userInput is lightweight and saved with rememberSaveable.
  • The ViewModel takes care of processing and persisting the submitted data.

Conclusion

The truth about ViewModel and rememberSaveable is simple once you think in terms of configuration changes vs process death:

  • remember → Only survives recomposition.
  • rememberSaveable → Survives both rotation and process death (small, serializable state).
  • ViewModel → Survives rotation, great for business logic, but not process death.

Use them in combination, not competition. Each tool has its place, and knowing when to reach for which makes your Compose apps more resilient, smoother, and user-friendly.

Kotlin Sequences

Kotlin Sequences or Java Streams? A Complete Guide for Modern Developers

If you’ve ever worked with collections in Kotlin or Java, you’ve probably heard about Kotlin Sequences and Java Streams. Both are powerful tools for handling large amounts of data in a clean, functional style. But when should you use one over the other? And what’s the real difference between them?

This guide breaks it all down in simple way — no jargon overload. By the end, you’ll know exactly when to reach for Kotlin Sequences or Java Streams in your projects.

Why Do We Even Need Sequences or Streams?

Collections like List and Set are everywhere. But looping through them with for or while can quickly become messy, especially when you want to:

  • Filter elements
  • Map values
  • Reduce results into a single outcome

This is where lazy evaluation comes in. Instead of processing all elements up front, sequences and streams let you chain operations in a pipeline. The work only happens when you actually need the result. That means cleaner code and often better performance.

Kotlin Sequences: Lazy by Design

Kotlin’s Sequence is basically a wrapper around collections that delays execution until the final result is requested.

Filtering and Mapping with Sequences

Kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)

    val result = numbers.asSequence()
        .filter { it % 2 == 1 }   // Keep odd numbers
        .map { it * it }          // Square them
        .toList()                 // Trigger evaluation

    println(result) // [1, 9, 25]
}

Here,

  • .asSequence() converts the list into a sequence.
  • filter and map are chained, but nothing actually runs yet.
  • .toList() triggers evaluation, so all steps run in a pipeline.

Key takeaway: Sequences process elements one by one, not stage by stage. That makes them memory-efficient for large datasets.

Java Streams: Functional Power in Java

Java introduced Stream in Java 8, giving developers a way to work with collections functionally.

Filtering and Mapping with Streams

Java
import java.util.*;
import java.util.stream.*;

public class StreamExample {
    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        List<Integer> result = numbers.stream()
            .filter(n -> n % 2 == 1)  // Keep odd numbers
            .map(n -> n * n)          // Square them
            .toList();                // Collect into a list

        System.out.println(result);   // [1, 9, 25]

    }
}

How It Works

  • .stream() converts the collection into a stream.
  • Operations like filter and map are chained.
  • .toList() (or .collect(Collectors.toList()) in older Java versions) triggers evaluation.

Streams are also lazy, just like Kotlin Sequences. But they come with a big advantage: parallel processing.

Kotlin Sequences vs Java Streams: Key Differences

Here’s a side-by-side comparison of Kotlin Sequences or Java Streams:

When to Use Kotlin Sequences

  • You’re writing Kotlin-first code.
  • You want simple lazy evaluation.
  • You’re processing large collections where memory efficiency matters.
  • You don’t need parallel execution.

Example: processing thousands of lines from a text file efficiently.

When to Use Java Streams

  • You’re in a Java-based project (or interoperating with Java).
  • You want parallel execution to speed up heavy operations.
  • You’re working with Java libraries that already return streams.

Example: data crunching across millions of records using parallelStream().

Which Should You Choose?

If you’re in Kotlin, stick with Kotlin Sequences. They integrate beautifully with the language and make your code feel natural.

If you’re in Java or need parallel execution, Java Streams are the way to go.

And remember: it’s not about one being “better” than the other — it’s about choosing the right tool for your context.

Conclusion

When it comes to Kotlin Sequences or Java Streams, the choice boils down to your project’s ecosystem and performance needs. Both give you lazy, functional pipelines that make code cleaner and more maintainable.

  • Kotlin developers → Sequences
  • Java developers or parallel workloads → Streams

Now you know when to use each one, and you’ve seen them in action with real examples. So the next time you need to process collections, you won’t just write a loop — you’ll reach for the right tool and make your code shine.

Artificial Neural Networks

Artificial Neural Networks Explained: How ANNs Mimic the Human Brain

Artificial Neural Networks (ANNs) are one of the driving forces behind today’s AI revolution. From recognizing faces in photos to powering voice assistants, they’re everywhere. But what exactly are they? And how do they mimic the human brain? Let’s break it down step by step.

What Are Artificial Neural Networks?

Artificial Neural Networks are computational models inspired by how the human brain processes information. Just like our brains use billions of interconnected neurons to learn and make decisions, ANNs use layers of artificial “neurons” to detect patterns, classify data, and make predictions.

At their core, ANNs are about finding relationships in data. Whether it’s images, text, or numbers, they can spot patterns we might miss.

How the Human Brain Inspires ANNs

The inspiration for ANNs comes directly from biology:

  • Neurons in the brain receive signals, process them, and pass them along if the signal is strong enough.
  • Artificial neurons work in a similar way: they take input, apply weights (importance), add them up, and pass the result through an activation function.

Think of it like this:

  • Neurons = nodes in a network.
  • Synapses = weights between nodes.
  • Brain learning = adjusting synapse strengths.
  • ANN learning = adjusting weights during training.

Anatomy of an Artificial Neural Network

Every ANN is built from layers:

  1. Input Layer — Where data enters the network.
     Example: pixels of an image.
  2. Hidden Layers — Where the “thinking” happens.
     These layers detect patterns, like edges, shapes, or textures.
  3. Output Layer — Where results are produced.
     Example: labeling an image as a “cat” or “dog.”

Each connection between neurons has a weight, and learning means updating those weights to improve accuracy.

How ANNs Learn: The Training Process

Training an ANN is like teaching a child. You show it examples, it makes guesses, and you correct it until it improves. Here’s the typical process:

  1. Forward Propagation — Data flows through the network, producing an output.
  2. Loss Calculation — The network checks how far its prediction is from the correct answer.
  3. Backward Propagation (Backprop) — The error flows backward through the network, adjusting weights to reduce mistakes.
  4. Repeat — This cycle happens thousands or even millions of times until the network becomes accurate.

A Simple Neural Network in Python

Let’s build a tiny ANN to classify numbers using TensorFlow and Keras. Don’t worry — it’s simpler than it looks.

Python
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Step 1: Build the model
model = Sequential([
    Dense(16, input_shape=(10,), activation='relu'),  # hidden layer with 16 neurons
    Dense(8, activation='relu'),                      # another hidden layer
    Dense(1, activation='sigmoid')                    # output layer (binary classification)
])

# Step 2: Compile the model
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# Step 3: Train the model with dummy data
import numpy as np
X = np.random.rand(100, 10)  # 100 samples, 10 features each
y = np.random.randint(2, size=100)  # 100 labels (0 or 1)
model.fit(X, y, epochs=10, batch_size=8)
  • Dense layers: These are fully connected layers where every neuron talks to every neuron in the next layer.
  • Activation functions: relu helps capture complex patterns; sigmoid squashes outputs between 0 and 1, making it great for yes/no predictions.
  • Optimizer (adam): Decides how the network updates its weights.
  • Loss function (binary_crossentropy): Measures how far off predictions are from actual results.
  • Training (fit): This is where learning happens—weights get adjusted to reduce errors.

Why Artificial Neural Networks Matter

Artificial Neural Networks power much of modern AI, including:

  • Image recognition (Google Photos, self-driving cars)
  • Natural language processing (chatbots, translation apps)
  • Healthcare (disease prediction, drug discovery)
  • Finance (fraud detection, stock predictions)

Their strength lies in adaptability: once trained, they can generalize knowledge and apply it to new, unseen data.

Challenges of ANNs

While powerful, ANNs have challenges:

  • Data hungry: They need lots of examples to learn.
  • Black box problem: It’s often hard to understand why a network makes certain decisions.
  • Computational cost: Training large ANNs requires heavy computing power.

Researchers are working on making them more efficient and interpretable.

Conclusion

Artificial Neural Networks are one of the best examples of how humans have borrowed ideas from nature — specifically the brain — to solve complex problems. They’re not truly “intelligent” in the human sense, but their ability to learn from data is transforming industries.

As we move forward, ANNs will continue to evolve, becoming more powerful and more transparent. Understanding the basics today means you’ll be ready for the AI-powered world of tomorrow.

What Are Browser Cookies

What Are Browser Cookies? What They Do, How They Work, and Are They Safe?

When you’re browsing the internet, you’ve probably seen pop-ups asking you to “accept cookies.” But what exactly are cookies? Are they helpful, harmless, or a privacy risk? Let’s break it down, so you can understand what’s happening behind the scenes every time you visit a website.

What Are Cookies?

Cookies are small text files that websites store on your browser. Think of them as a memory card for the web. They help websites “remember” who you are, what you like, and what you did the last time you visited.

For example:

  • When you log in to a site and it keeps you signed in the next time, that’s cookies at work.
  • When an online store remembers what’s in your shopping cart, that’s also cookies.

They don’t contain software or viruses. Instead, they’re just data — like notes websites leave for themselves.

How Do Cookies Work?

When you visit a website, the site sends a small piece of data (the cookie) to your browser. Your browser then stores this file and sends it back to the website on your next visit. This back-and-forth helps the site recognize you.

Here’s a simple illustration using code:

JavaScript
// Set a cookie
document.cookie = "username=amoljp19; expires=Fri, 31 Dec 2025 23:59:59 GMT; path=/";

// Read cookies
console.log(document.cookie);

// Delete a cookie (by setting it to expire in the past)
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
  • document.cookie lets you create, read, and delete cookies in your browser.
  • The expires part tells the browser when the cookie should be deleted automatically.
  • path=/ means the cookie works across the entire website.

So, cookies are essentially key-value pairs (like username=amoljp19) that websites use to save information about your activity.

Types of Cookies

Not all cookies are the same. Here are the most common ones:

  1. Session Cookies — Temporary cookies that disappear when you close your browser. Used for things like keeping you logged in while you move between pages.
  2. Persistent Cookies — Stay stored on your device until they expire or you delete them. These are often used for remembering login details or preferences.
  3. First-Party Cookies — Created by the website you’re visiting directly. Generally safe and useful.
  4. Third-Party Cookies — Created by advertisers or external services. These track your activity across multiple websites, which is why ads sometimes feel “too personalized.”

Why Do Websites Use Cookies?

Websites use cookies for many reasons, including:

  • Authentication: Remembering who you are after login.
  • Personalization: Saving preferences like language or theme.
  • Analytics: Tracking how visitors use the site to improve user experience.
  • Advertising: Targeting ads based on your browsing behavior.

Are Cookies Safe?

For the most part, cookies are safe. They can’t install viruses or run malicious code. But there are some privacy concerns:

  • Tracking: Third-party cookies can monitor your activity across websites, which raises concerns about personal data.
  • Data Exposure: If a website doesn’t handle cookies securely (e.g., without encryption), hackers could potentially access your information.

That’s why modern browsers give you options to manage cookies — block them, allow them selectively, or delete them anytime.

How to Manage Cookies

You’re in control of cookies. Here’s how you can manage them:

  • Delete cookies: Clear them regularly through your browser’s settings.
  • Block third-party cookies: Most browsers let you block tracking cookies while still allowing first-party cookies.
  • Use incognito/private mode: Cookies are deleted automatically when you close the window.

Example: In Google Chrome, go to Settings → Privacy and Security → Cookies and Other Site Data to manage them.

The Future of Cookies

Big changes are happening. Many browsers (like Safari and Firefox) already block third-party cookies by default. Google Chrome is also phasing them out in favor of Privacy Sandbox, a new approach to balance ads with user privacy.

So, while cookies have been around since the 1990s, their role is evolving.

FAQ About Cookies

1. Do cookies store my passwords?

Not directly. Cookies may store a session token that keeps you logged in, but your actual password is not saved inside cookies. Passwords are usually stored securely on the website’s server.

2. Can I browse the web without cookies?

Yes. You can disable cookies in your browser settings or use incognito mode. However, some websites may not work properly — for example, you may get logged out frequently or lose shopping cart items.

3. Are cookies the same as cache?

No. Cookies store small pieces of user-specific data, while cache stores website resources (like images, CSS, or scripts) to load pages faster.

4. Can cookies track me across websites?

Only third-party cookies can do that. They’re mainly used for advertising and analytics. Many browsers now block them by default.

5. Should I delete cookies regularly?

It depends. If you value privacy, deleting cookies often is a good habit. But if you enjoy convenience (like staying logged in), you may want to keep first-party cookies.

Key Takeaways

  • Cookies are small files that store your online activity and preferences.
  • They help websites remember you, personalize your experience, and run smoothly.
  • They’re generally safe, but third-party cookies can track you across sites.
  • You have control — delete or block cookies anytime in your browser.

Conclusion 

Cookies aren’t scary. They’re a tool that makes the web more convenient. But like all tools, they need to be used responsibly. Stay informed, manage your settings, and enjoy a safer, smoother browsing experience.

From Zero to PHP Hero

From Zero to PHP Hero: Essential Basics for New Coders

If you’ve ever wanted to build dynamic websites, you’ve probably heard about PHP. Despite newer languages entering the web development space, PHP remains a powerhouse for powering eCommerce sites, and even large platforms like Facebook (in its early days), and Wikipedia.

The best part? PHP is beginner-friendly. You don’t need to be a coding wizard to get started — you just need curiosity, patience, and the right guidance. In this post, we’ll walk through the essential basics of PHP, explain concepts with examples, and give you a strong foundation to go from zero to PHP hero.

What Is PHP?

PHP stands for Hypertext Preprocessor. It’s a server-side scripting language designed for web development. Unlike HTML (which is static), PHP allows you to create interactive and dynamic pages.

For example, HTML can show a webpage, but PHP can:

  • Connect to a database.
  • Process form submissions.
  • Generate content dynamically.

Think of HTML as the stage, and PHP as the actor who makes things happen.

Setting Up PHP

Before writing code, you’ll need an environment where PHP can run. There are two easy ways:

  1. Install XAMPP or WAMP — These tools bundle PHP, Apache (server), and MySQL (database). Perfect for local development.
  2. Use an Online PHP Sandbox — Sites like php-fiddle let you try PHP without installation.

Once you’re set up, you can write .php files and run them through your server.

Your First PHP Script

Let’s start simple: printing text to a webpage.

Kotlin
<?php
  echo "Hello, World!";
?>
  • <?php ... ?> is the PHP tag. All PHP code goes inside.
  • echo outputs text to the browser.
  • "Hello, World!" is a string we’re displaying.

When you run this, your browser will show:

Kotlin
Hello, World!

PHP Variables

Variables are like containers for storing data. In PHP, variables start with a $ sign.

Kotlin
<?php
  $name = "amol";
  $age = 25;

echo "My name is $name and I am $age years old.";
?>
  • $name stores a string.
  • $age stores a number.
  • Double quotes (" ") allow variables to be used directly inside the string.

Output:

PHP
My name is amol and I am 25 years old.

Data Types in PHP

PHP supports several data types, including:

  • String: Text ("Hello")
  • Integer: Whole numbers (42)
  • Float: Decimal numbers (3.14)
  • Boolean: True/False
  • Array: List of values
  • Object: Instance of a class

Example with arrays:

PHP
<?php
  $colors = array("Red", "Green", "Blue");
  echo $colors[0]; // Prints Red
?>

Arrays are great for storing multiple values in a single variable.

PHP Operators

Operators let you perform calculations or comparisons.

PHP
<?php
  $x = 10;
  $y = 5;

  echo $x + $y; // 15
  echo $x - $y; // 5
  echo $x * $y; // 50
  echo $x / $y; // 2
?>

You can also compare values:

PHP
<?php
  var_dump($x > $y); // true
  var_dump($x == $y); // false
?>

Conditional Statements

PHP makes your site dynamic with if-else conditions.

PHP
<?php
  $score = 85;

  if ($score >= 90) {
    echo "Grade: A";
  } elseif ($score >= 75) {
    echo "Grade: B";
  } else {
    echo "Grade: C";
  }
?>

This script checks the $score and prints the grade.

Loops in PHP

Loops let you repeat tasks without writing the same code multiple times.

For loop example:

PHP
<?php
  for ($i = 1; $i <= 5; $i++) {
    echo "Number: $i <br>";
  }
?>

Output:

PHP
Number: 1  
Number: 2  
Number: 3  
Number: 4  
Number: 5

PHP Functions

Functions help organize reusable code.

PHP
<?php
  function greet($name) {
    return "Hello, $name!";
  }

  echo greet("amol");
?>

Output:

Kotlin
Hello, amol!

Connecting PHP with HTML

The real magic happens when you mix PHP with HTML.

PHP
<!DOCTYPE html>
<html>
<head>
  <title>PHP Example</title>
</head>
<body>
  <h1>Welcome!</h1>
  <p>
    <?php
      $user = "amol";
      echo "Hello, $user. Glad you're here!";
    ?>
  </p>
</body>
</html>

When loaded, the browser will show:

PHP
Welcome!  
Hello, amol. Glad you're here!

This is why PHP is so powerful — it can seamlessly interact with HTML.

Next Steps to Become a PHP Hero

Now that you’ve learned the essentials, here’s how to level up:

  • Work with Forms: Handle user input.
  • Learn MySQL: Store and fetch data with PHP.
  • Understand Sessions & Cookies: Manage logins and preferences.
  • Practice Small Projects: Start with a simple login page, contact form, or guestbook.

Conclusion

Going from zero to PHP hero isn’t about learning everything at once. It’s about taking small steps and building confidence. Start with the basics we covered — variables, loops, conditions, and functions — and gradually move to more complex topics like databases and authentication.

Remember: even the best developers once wrote their first echo "Hello, World!";. With consistent practice, you’ll be writing dynamic, real-world PHP applications in no time.

CompositionLocal

CompositionLocal Deep Dive: Writing Scalable UI in Jetpack Compose

When building Android apps with Jetpack Compose, you’ll often need to share data across your UI tree. Passing parameters down every Composable quickly becomes messy. That’s where CompositionLocal comes in.

Think of CompositionLocal as a smart way to provide values (like theme, locale, or user preferences) to multiple Composables without having to manually thread them through function parameters. It’s like dependency injection — but scoped to the Compose world.

In this post, we’ll explore how CompositionLocal works, why it matters for building scalable UI, and how you can use it effectively.

What is CompositionLocal?

CompositionLocal is a mechanism that allows you to define and access values that are automatically propagated down the Composable hierarchy.

  • It provides contextual values (like theme colors or configurations).
  • It removes the need to pass arguments everywhere.
  • It helps you scale UI architecture by keeping components decoupled.

Jetpack Compose already uses CompositionLocal under the hood for things like MaterialTheme, text styles, and layout direction.

Defining a CompositionLocal

You start by creating a CompositionLocal with a default value:

Kotlin
val LocalUser = compositionLocalOf<String> { 
    error("No user provided") 
}

Here:

  • compositionLocalOf creates a CompositionLocal with a default (or error if missing).
  • We’re saying: “If no user is provided, throw an error.”

Providing a Value

To inject a value, you use CompositionLocalProvider:

Kotlin
@Composable
fun AppContent() {
    CompositionLocalProvider(LocalUser provides "amol pawar") {
        UserProfile()
    }
}

Inside AppContent, any child Composable can access LocalUser.

Consuming a CompositionLocal

To read the value, use .current:

Kotlin
@Composable
fun Dashboard() {
    Column {
        CompositionLocalProvider(LocalUser provides "akshay") {
            UserProfile() // shows "Hello, askhay!"
        }
        UserProfile() // shows "Hello, amol pawar!"
    }
}

Output:

Hello, amol pawar!

No need to pass user down as a parameter—CompositionLocal handles it.

Why Use CompositionLocal?

Let’s break it down with a practical example. Imagine a large app with:

  • Theme data (colors, typography).
  • User session info.
  • App settings like dark mode, locale, etc.

Passing these manually would be a nightmare. With CompositionLocal, you define them once and let the UI tree consume them where needed.

Scoped Values for Flexibility

One powerful feature is scoping. You can override a CompositionLocal in a subtree without affecting the rest of the app.

Kotlin
@Composable
fun Dashboard() {
    Column {
        CompositionLocalProvider(LocalUser provides "akshay") {
            UserProfile() // shows "Hello, askhay!"
        }
        UserProfile() // shows "Hello, amol pawar!"
    }
}

The value depends on where the Composable is in the hierarchy. This makes it perfect for context-specific overrides (like previewing different themes).

Best Practices for CompositionLocal

  1. Don’t abuse it. Use CompositionLocal for global or contextual data, not just to avoid passing parameters.
  2. Keep defaults meaningful. Provide safe defaults or throw an error if the value is critical.
  3. Use for ambient context. Theme, locale, user, system settings — these are ideal use cases.
  4. Avoid hidden dependencies. If a Composable always needs a value, prefer explicit parameters for clarity.

Theme System with CompositionLocal

Let’s create a mini theme system:

Kotlin
data class MyColors(val primary: Color, val background: Color)

val LocalColors = staticCompositionLocalOf<MyColors> {
    error("No colors provided")
}

@Composable
fun MyTheme(content: @Composable () -> Unit) {
    val colors = MyColors(primary = Color.Blue, background = Color.White)
    CompositionLocalProvider(LocalColors provides colors) {
        content()
    }
}

@Composable
fun ThemedButton() {
    val colors = LocalColors.current
    Button(onClick = {}) {
        Text("Click Me", color = colors.primary)
    }
}

Usage:

Kotlin
@Composable
fun App() {
    MyTheme {
        ThemedButton()
    }

Here, ThemedButton gets its styling from LocalColors without needing parameters.

CompositionLocal vs Parameters

  • Use parameters when data is essential to the Composable.
  • Use CompositionLocal when data is contextual, like theming or configuration.

This balance keeps your UI scalable and maintainable.

Conclusion

CompositionLocal is one of the most powerful tools in Jetpack Compose for writing scalable UI. It keeps your code cleaner, reduces boilerplate, and makes context handling a breeze.

By using CompositionLocal wisely, you can:

  • Share contextual data easily
  • Override values locally
  • Keep UI components decoupled and reusable

Next time you’re passing a value through five different Composables, stop and ask yourself — could CompositionLocal handle this better?

Scrollable Sticky Table/Grid UIs in Jetpack Compose

Build Scrollable Sticky Table/Grid UIs in Jetpack Compose Like a Pro

If you’ve ever built dashboards, spreadsheets, or financial apps, you know how important tables are. But a plain table isn’t enough — you often need a Scrollable Sticky Table/Grid where headers stay in place while data scrolls smoothly. In the past, building this in Android XML layouts was painful. With Jetpack Compose, you can achieve it with...

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here
Jetpack Compose LazyColumn Sticky Header

Jetpack Compose LazyColumn Sticky Header: Complete Implementation Guide

When you’re building long lists in Jetpack Compose, sometimes you need certain sections to stand out and stay visible while scrolling. That’s exactly where Sticky Header comes in. Imagine scrolling through a contacts app — the alphabet letter headers (A, B, C…) stick at the top while you browse through names. Jetpack Compose makes this easy with LazyColumn and stickyHeader.

In this guide, I’ll walk you through how to implement Sticky Header in Jetpack Compose with clear explanations.

What is a Sticky Header?

A Sticky Header is a UI element that “sticks” at the top of a scrollable list until the next header pushes it off. It’s commonly used in:

  • Contact lists
  • Calendar apps
  • Shopping category lists
  • News feeds with date separators

This improves navigation and makes large lists easier to scan.

Why Use Sticky Header in Jetpack Compose?

With Jetpack Compose, you don’t need RecyclerView adapters or complex custom views. The LazyColumn component handles large, scrollable lists efficiently, and stickyHeader makes adding sticky sections straightforward.

Benefits:

  • Simple syntax, no XML layouts.
  • Clean and declarative code.
  • Works seamlessly with Compose state management.

LazyColumn and stickyHeader Basics

Here’s the basic structure of a LazyColumn with a Sticky Header:

Kotlin
@Composable
fun StickyHeaderExample() {
    val sections = listOf(
        "Fruits" to listOf("Apple", "Banana", "Orange"),
        "Vegetables" to listOf("Carrot", "Potato", "Tomato"),
        "Dairy" to listOf("Milk", "Cheese", "Yogurt")
    )

    LazyColumn {
        sections.forEach { (header, items) ->
            stickyHeader {
                Text(
                    text = header,
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(Color.LightGray)
                        .padding(8.dp),
                    style = MaterialTheme.typography.subtitle1
                )
            }

            items(items) { item ->
                Text(
                    text = item,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                )
            }
        }
    }
}

Let’s break it down:

Data Setup

Kotlin
val sections = listOf(
    "Fruits" to listOf("Apple", "Banana", "Orange"),
    "Vegetables" to listOf("Carrot", "Potato", "Tomato"),
    "Dairy" to listOf("Milk", "Cheese", "Yogurt")
)

Here, each section has a header (like “Fruits”) and a list of items.

LazyColumn

Kotlin
LazyColumn { ... }

Displays the entire list efficiently. Only visible items are composed, so it’s memory-friendly.

stickyHeader

Kotlin
stickyHeader {
    Text(
        text = header,
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.LightGray)
            .padding(8.dp)
    )
}

This is the star of the show. The header stays pinned at the top while scrolling through its section.

items()

Kotlin
items(items) { item -> ... }

Renders each element under the sticky header.

Customizing Sticky Headers

You can style sticky headers to fit your app’s design. For example:

  • Add icons to headers.
  • Change background color based on section.
  • Apply elevation or shadows for better separation.

Example with custom styling:

Kotlin
stickyHeader {
    Surface(
        color = Color.DarkGray,
        shadowElevation = 4.dp
    ) {
        Text(
            text = header,
            modifier = Modifier
                .fillMaxWidth()
                .padding(12.dp),
            color = Color.White,
            style = MaterialTheme.typography.h6
        )
    }
}

When to Use and When Not To

Use Sticky Header when:

  • The list is grouped (categories, dates, sections).
  • Users need quick context while scrolling.

Avoid Sticky Header if:

  • The list is flat (no categories).
  • Too many headers clutter the UI.

Performance Considerations

LazyColumn with stickyHeader is optimized, but keep these in mind:

  • Keep headers lightweight (avoid heavy Composables inside).
  • Reuse stateful items outside of the list when possible.
  • Test on lower-end devices if you have very large datasets.

Conclusion

The Sticky Header in Jetpack Compose makes complex, sectioned lists much easier to build and navigate. With just a few lines of code inside a LazyColumn, you can create polished, user-friendly experiences without dealing with RecyclerView boilerplate.

If you’re building apps with grouped data — contacts, shopping categories, or event timelines — Sticky Header is a feature you’ll definitely want to use.

Sticky Header in Jetpack Compose

How to Create a Sticky Header in Jetpack Compose

If you’ve ever scrolled through a long list in an app and noticed that the section title stays pinned at the top until the next section appears, you’ve seen a sticky header. Sticky headers make lists easier to navigate, especially when content is grouped by category.

In this post, we’ll learn step by step how to implement a Sticky Header in Jetpack Compose using LazyColumn. Don’t worry if you’re just getting started with Compose—the explanation will stay simple, with code examples and clear breakdowns.

What is a Sticky Header?

A sticky header is a UI element that remains visible at the top of a scrolling list while the content beneath it scrolls. It’s often used in apps like Contacts (where the alphabet letters stick as you scroll) or e-commerce apps (where categories like “Shoes,” “Bags,” or “Clothing” stay pinned).

Jetpack Compose makes this much easier to implement compared to the old RecyclerView approach in XML.

Why Use Sticky Headers in Jetpack Compose?

Adding a Sticky Header in Jetpack Compose improves:

  • Readability: Users instantly know which section they’re in.
  • Navigation: Helps users scan through grouped content quickly.
  • User Experience: Feels modern, polished, and professional.

The Key Composable: stickyHeader

Jetpack Compose provides a built-in modifier inside LazyColumn called stickyHeader. This allows you to define a composable item that “sticks” to the top while scrolling.

Basic Code Example

Here’s a simple example of creating a Sticky Header in Jetpack Compose:

Kotlin
@Composable
fun StickyHeaderList() {
    val groupedItems = mapOf(
        "Fruits" to listOf("Apple", "Banana", "Mango", "Orange"),
        "Vegetables" to listOf("Carrot", "Potato", "Tomato"),
        "Drinks" to listOf("Water", "Juice", "Soda")
    )

    LazyColumn {
        groupedItems.forEach { (header, items) ->
            stickyHeader {
                Text(
                    text = header,
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(Color.LightGray)
                        .padding(16.dp),
                    fontWeight = FontWeight.Bold
                )
            }

            items(items) { item ->
                Text(
                    text = item,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(12.dp)
                )
            }
        }
    }
}

Let’s break it down so it’s crystal clear:

Grouped Data

  • We created a Map with categories as keys ("Fruits", "Vegetables", "Drinks") and a list of items under each.

LazyColumn

  • Works like a RecyclerView but in Compose. It’s efficient for large lists.

stickyHeader

  • This is the magic. Whatever you put inside stickyHeader will remain stuck at the top until another header replaces it.
  • We used a Text with background color and padding so it looks like a section header.

items()

  • Displays each element in the list under its header.

Styling the Sticky Header

You don’t want your sticky header to look boring. Here are a few tweaks you can add:

Kotlin
stickyHeader {
    Surface(
        modifier = Modifier.fillMaxWidth(),
        color = Color.DarkGray,
        shadowElevation = 4.dp
    ) {
        Text(
            text = header,
            modifier = Modifier.padding(16.dp),
            color = Color.White,
            fontSize = 18.sp,
            fontWeight = FontWeight.Bold
        )
    }
}

This adds:

  • Background color (DarkGray)
  • Shadow elevation for depth
  • White text for contrast

When to Use Sticky Headers

Sticky headers are perfect for:

  • Contact lists grouped alphabetically
  • Shopping apps with categories
  • News apps with sections (e.g., Sports, Tech, Business)
  • Music playlists grouped by artist or album

Common Mistakes to Avoid

  • Too many sticky headers: Don’t overuse them — it can feel cluttered.
  • No visual distinction: Make sure headers look different from list items.
  • Performance issues: For extremely large datasets, consider lazy loading.

Conclusion

Creating a Sticky Header in Jetpack Compose is simple, thanks to the stickyHeader API inside LazyColumn. With just a few lines of code, you can build a smooth, user-friendly list that looks polished and professional.

As Compose continues to evolve, features like these make UI development faster, cleaner, and more intuitive. Whether you’re building a contacts app, a shopping app, or just experimenting, sticky headers will give your lists a better structure and improve the user experience.

Pro Tip: Always test on different screen sizes to make sure your headers remain clear and readable.

Now it’s your turn — try adding a sticky header to your own Jetpack Compose project and see the difference!

Doubly Linked List in Kotlin

Doubly Linked List in Kotlin: Real-World Use Cases and Code Snippets

When working with data structures in Kotlin, arrays and lists often come to mind first. They’re built-in, simple, and cover most scenarios. But sometimes you need more control over how elements are connected, inserted, or removed. That’s where a Doubly Linked List in Kotlin shines.

In this blog, we’ll explore what a doubly linked list is, why it’s useful, real-world applications, and most importantly — how to implement one in Kotlin.

Doubly Linked List in Kotlin

A doubly linked list is a data structure made up of nodes. Each node stores three things:

  1. Data — the actual value.
  2. A reference to the next node.
  3. A reference to the previous node.

This dual-link system allows navigation forward and backward through the list. That’s the main difference from a singly linked list, which only moves forward.

Why Use a Doubly Linked List in Kotlin?

You might ask: “Why bother with a doubly linked list when Kotlin already has List and MutableList?”

Here are a few reasons:

  • Fast insertions and deletions: Unlike arrays, you don’t need to shift elements when adding or removing.
  • Bidirectional traversal: You can move in both directions, which can be handy in scenarios like undo/redo features.
  • Custom data structures: Sometimes you want full control over memory and connections.

Real-World Use Cases

Let’s look at where a Doubly Linked List in Kotlin can be practical:

  • Browser history navigation (go back and forward between pages).
  • Undo/Redo operations in editors.
  • Music playlists where you can jump to the previous or next song.
  • Deque (Double-Ended Queue) implementations for efficient queue operations.

Implementing a Doubly Linked List in Kotlin

Let’s write a clean, easy-to-follow implementation.

Define the Node

Java
class Node<T>(
    var data: T,
    var prev: Node<T>? = null,
    var next: Node<T>? = null
)

Here, Node is generic (<T>) so it can store any type (Int, String, custom objects, etc.). Each node keeps track of its data, the previous node (prev), and the next node (next).

Create the DoublyLinkedList Class

Java
class DoublyLinkedList<T> {
    private var head: Node<T>? = null
    private var tail: Node<T>? = null

    fun isEmpty() = head == null
}

We keep track of two references:

  • head → the first node.
  • tail → the last node.

Add Elements

Let’s add items to the end of the list.

Java
fun append(data: T) {
    val newNode = Node(data)

    if (head == null) {
        head = newNode
        tail = newNode
    } else {
        tail?.next = newNode
        newNode.prev = tail
        tail = newNode
    }
}
  • If the list is empty, both head and tail point to the new node.
  • Otherwise, we connect the new node after the current tail and update tail.

Prepend Elements

Adding to the beginning works similarly:

Kotlin
fun prepend(data: T) {
    val newNode = Node(data)

    if (head == null) {
        head = newNode
        tail = newNode
    } else {
        newNode.next = head
        head?.prev = newNode
        head = newNode
    }
}

Remove Elements

Removing a node requires updating both previous and next references.

Kotlin
fun remove(data: T) {
    var current = head

    while (current != null) {
        if (current.data == data) {
            if (current.prev != null) {
                current.prev?.next = current.next
            } else {
                head = current.next
            }

            if (current.next != null) {
                current.next?.prev = current.prev
            } else {
                tail = current.prev
            }
            break
        }
        current = current.next
    }
}

Here we search for the node, reconnect neighbors around it, and update head or tail if needed.

Print the List

For debugging, let’s add a print function.

Kotlin
fun printForward() {
    var current = head
    while (current != null) {
        print("${current.data} ")
        current = current.next
    }
    println()
}

fun printBackward() {
    var current = tail
    while (current != null) {
        print("${current.data} ")
        current = current.prev
    }
    println()
}

Full Example in Action

Before running the code, make sure all the above functions are inside the DoublyLinkedList<T> class.

Kotlin
fun main() {
    val list = DoublyLinkedList<Int>()

    list.append(10)
    list.append(20)
    list.append(30)
    list.prepend(5)

    println("Forward traversal:")
    list.printForward()

    println("Backward traversal:")
    list.printBackward()

    println("Removing 20...")
    list.remove(20)
    list.printForward()
}

Output:

Kotlin
Forward traversal:
5 10 20 30 
Backward traversal:
30 20 10 5 
Removing 20...
5 10 30 

Conclusion

A Doubly Linked List in Kotlin gives you more control when working with dynamic data. While Kotlin’s standard library handles most needs with List or MutableList, knowing how to build and use a doubly linked list can be a powerful skill.

You now know:

  • What a doubly linked list is.
  • Real-world scenarios where it’s useful.
  • How to implement it step by step in Kotlin.

This structure shines in apps where insertion, deletion, or bidirectional navigation matters — like history tracking, playlists, or undo/redo stacks.

error: Content is protected !!