Amol Pawar

Architecting Alarms & Notifications in Android

Architecting Alarms & Notifications in Android: The Clean Architecture Way

Alarms, reminders, and notifications are some of the most deceptively complex features in Android development.

On the surface, they look simple: “Schedule something at a time and show a notification.”

In reality, you’re fighting with:

  • Doze mode
  • OEM background restrictions
  • Process death
  • App restarts
  • Device reboots
  • Android 12+ background execution limits
  • Android 13+ notification permissions
  • Android 14+ stricter alarms policies

After more than a decade building Android apps at scale, one thing is clear:

Most alarm and notification bugs are architectural bugs, not API bugs.

This article walks through how to architect alarms and notifications in a real production app using MVVM + Clean Architecture, feature modules, and a hybrid AlarmManager + WorkManager approach that survives modern Android constraints.

This is not theory. This is how you ship reliable reminder systems.

Core Architectural Principles (Hard-Earned Lessons)

Before talking about code, we need to align on principles. These are non-negotiable.

  1. Business logic must not depend on Android
  2. Scheduling is a domain concern, execution is an infrastructure concern
  3. Alarms wake the app, Workers do the work
  4. Repeating alarms are not repeating system alarms
  5. Everything must survive process death and reboot

If your current implementation violates any of these, instability is inevitable.

High-Level Architecture Overview

At a conceptual level, the flow looks like this:

Kotlin
UI → ViewModel → UseCase → Domain → Scheduler abstraction

                               Android implementation

                          AlarmManager → Receiver → Worker

                                Notification

Each arrow represents a boundary. Boundaries are what give us control.

Feature-Based Modular Architecture

In real apps, packages are not enough. Gradle modules matter.

Recommended Module Layout

Kotlin
:app

:core:domain
:core:data
:core:infra
:core:common

:feature:reminder
:feature:notification

Why This Matters

  • Domain becomes completely platform-independent
  • Feature modules own UI and presentation logic
  • Infra absorbs Android volatility
  • Teams can work independently without merge hell

Dependency Rules (Strict)

Kotlin
feature → domain
data → domain
infra → domain + data
app → everything

If a feature imports AlarmManager, your architecture is already compromised.

Domain Layer: Where Truth Lives

The domain layer is pure Kotlin. No Context. No Android imports.

Reminder Model

Kotlin
data class Reminder(
    val id: String,
    val triggerAtMillis: Long,
    val title: String,
    val message: String,
    val repeat: RepeatRule?
)

This model expresses intent, not implementation.

Repeat Rule Modeling (Critical Design)

Repeating reminders are the most common source of bugs. The fix starts here.

Kotlin
sealed class RepeatRule {
    data class Daily(val intervalDays: Int = 1) : RepeatRule()
    data class Weekly(val daysOfWeek: Set<DayOfWeek>) : RepeatRule()
    data class Interval(val millis: Long) : RepeatRule()
}

This gives us:

  • Explicit behavior
  • Full control
  • Deterministic scheduling

Scheduler Abstraction

The domain does not care how scheduling happens.

Kotlin
interface ReminderScheduler {
    fun schedule(reminder: Reminder)
    fun cancel(reminderId: String)
}

This single interface is what keeps Android chaos contained.

Use Case Example

Kotlin
class ScheduleReminderUseCase(
    private val repository: ReminderRepository,
    private val scheduler: ReminderScheduler
) {
    suspend operator fun invoke(reminder: Reminder) {
        repository.save(reminder)
        scheduler.schedule(reminder)
    }
}

Notice the order:

  1. Persist
  2. Schedule

That one decision is the difference between recoverable and broken reminders.

Presentation Layer (MVVM Done Properly)

The ViewModel knows nothing about alarms or notifications.

Kotlin
class ReminderViewModel(
    private val scheduleReminder: ScheduleReminderUseCase
) : ViewModel() {

   fun schedule(time: Long, title: String) {
        viewModelScope.launch {
            scheduleReminder(
                Reminder(
                    id = UUID.randomUUID().toString(),
                    triggerAtMillis = time,
                    title = title,
                    message = "Reminder",
                    repeat = null
                )
            )
        }
    }
}

This is clean, testable, and boring — exactly how ViewModels should be.

Infrastructure Layer: Where Android Lives

This is the layer that absorbs:

  • AlarmManager
  • PendingIntent
  • BroadcastReceiver
  • WorkManager
  • Notifications

And keeps them out of your business logic.

Alarm Scheduling Implementation

Kotlin
class AndroidReminderScheduler(
    private val alarmManager: AlarmManager,
    private val pendingIntentFactory: ReminderPendingIntentFactory
) : ReminderScheduler {

    override fun schedule(reminder: Reminder) {
        alarmManager.setExactAndAllowWhileIdle(
            AlarmManager.RTC_WAKEUP,
            reminder.triggerAtMillis,
            pendingIntentFactory.create(reminder.id)
        )
    }

    override fun cancel(reminderId: String) {
        alarmManager.cancel(
            pendingIntentFactory.create(reminderId)
        )
    }
}

We use:

  • setExactAndAllowWhileIdle
  • Explicit PendingIntent
  • One alarm per reminder

Anything else is unreliable.

PendingIntent Factory (Often Overlooked)

Kotlin
class ReminderPendingIntentFactory(
    private val context: Context
) {
    fun create(id: String): PendingIntent {
        val intent = Intent(context, ReminderAlarmReceiver::class.java)
            .putExtra("REMINDER_ID", id)

        return PendingIntent.getBroadcast(
            context,
            id.hashCode(),
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
    }
}

Correct requestCode usage is essential for cancellation and updates.

Alarm Receiver: Do Almost Nothing

Kotlin
class ReminderAlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val work = OneTimeWorkRequestBuilder<ReminderWorker>()
            .setInputData(workDataOf(
                "REMINDER_ID" to intent.getStringExtra("REMINDER_ID")
            ))
            .build()

        WorkManager.getInstance(context).enqueue(work)
    }
}

Receivers should delegate immediately. Anything else risks ANRs.

Worker: The Real Execution Engine

This is where notifications are shown and repeats are handled.

Kotlin
@HiltWorker
class ReminderWorker @AssistedInject constructor(
    @Assisted context: Context,
    @Assisted params: WorkerParameters,
    private val repository: ReminderRepository,
    private val scheduler: ReminderScheduler,
    private val calculator: NextTriggerCalculator
) : CoroutineWorker(context, params) {

     override suspend fun doWork(): Result {
        val id = inputData.getString("REMINDER_ID") ?: return Result.failure()
        val reminder = repository.getById(id) ?: return Result.failure()
        showNotification(reminder)
        reminder.repeat?.let {
            val next = calculator.calculate(reminder.triggerAtMillis, it)
            val updated = reminder.copy(triggerAtMillis = next)
            repository.save(updated)
            scheduler.schedule(updated)
        }
        return Result.success()
    }
}

This explicit rescheduling is what makes repeating reminders reliable.

Why Repeating Alarms Must Be Manual

Using setRepeating():

  • Is inexact
  • Breaks under Doze
  • Is ignored by OEMs
  • Cannot adapt dynamically

One alarm → one execution → reschedule next

This model survives every Android version.

Hilt Dependency Injection (Exact Bindings)

Scheduler Binding

Kotlin
@Module
@InstallIn(SingletonComponent::class)
object SchedulerModule {

    @Provides
    fun provideReminderScheduler(
        alarmManager: AlarmManager,
        factory: ReminderPendingIntentFactory
    ): ReminderScheduler =
        AndroidReminderScheduler(alarmManager, factory)
}

AlarmManager Binding

Kotlin
@Module
@InstallIn(SingletonComponent::class)
object SystemServiceModule {

    @Provides
    fun provideAlarmManager(
        @ApplicationContext context: Context
    ): AlarmManager =
        context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
}

Domain remains DI-agnostic.

AlarmManager vs WorkManager: The Real Decision Matrix

RequirementAlarmManagerWorkManager
Exact timingProvides precise, exact-time execution and is suitable for alarms and reminders that must fire at a specific momentDoes not guarantee exact timing; execution can be delayed based on system conditions
Doze compatibilityCan fire during Doze mode only when using exact alarms with allow-while-idle, and even then execution is constrainedDoes not run during Doze at an exact time and may be deferred until the system exits idle mode
Long background workNot suitable for long-running background tasks; execution time must be very shortDesigned specifically for longer background work with proper lifecycle handling
Survives device rebootDoes not automatically survive a device reboot and requires manual reschedulingAutomatically survives device reboot and restores scheduled work
Battery efficiencyLess battery efficient because it bypasses system optimizations for exact timingMore battery efficient because it respects system scheduling and optimization policies
User-visible alertsWell suited for user-visible actions such as alarms, reminders, and time-critical notificationsLess reliable for user-visible alerts that must appear at an exact time
  • Use AlarmManager when the user expects something to happen at an exact moment, such as reminders, alarms, or medication alerts.
  • Use WorkManager when the task involves background processing, network calls, database work, or any operation that does not require exact timing.
  • Combine both for production-grade systems: let AlarmManager wake the app, and let WorkManager perform the actual work.

The Correct Hybrid Approach

  1. AlarmManager wakes the app at the exact time
  2. WorkManager executes background work
  3. Worker shows notification and reschedules

This combination works across:

  • Android 8 → 15
  • OEM restrictions
  • Play Store policies

Conclusion

Reliable alarms and notifications are not about clever tricks or undocumented APIs. They are about respecting architectural boundaries and accepting Android’s reality.

If you:

  • Keep domain pure
  • Treat scheduling as a business concern
  • Let infra absorb platform volatility
  • Reschedule explicitly

Your reminder system will survive every Android release.

And you’ll sleep better knowing your users won’t miss their alarms.

Factory Pattern Simplify Complex Object Creation

How Does the Factory Pattern Simplify Complex Object Creation in Modern Applications?

Creating objects sounds simple at first. You call a constructor, pass a few values, and move on.

But as applications grow, object creation often becomes messy.

You start seeing:

  • Too many if-else or when blocks
  • Classes tightly coupled to concrete implementations
  • Code that’s hard to test, extend, or understand

This is exactly where the Factory Pattern Simplify Complex Object Creation problem in modern applications.

Let’s break it down in a clear and practical way.

What Is the Factory Pattern?

The Factory Pattern is a creational design pattern that handles object creation for you.

Instead of creating objects directly using constructors, you delegate that responsibility to a factory.

In simple terms:

A factory decides which object to create and how to create it.

Your main code just asks for an object and uses it. It doesn’t care about the details.

This separation is what makes the Factory Pattern Simplify Complex Object Creation so effective.

Why Object Creation Becomes Complex

In real-world applications, object creation often depends on:

  • User input
  • Configuration files
  • API responses
  • Environment (development, testing, production)

Example without a factory:

Kotlin
val paymentProcessor = when (paymentType) {
    "CARD" -> CardPaymentProcessor()
    "UPI" -> UpiPaymentProcessor()
    "WALLET" -> WalletPaymentProcessor()
    else -> throw IllegalArgumentException("Invalid payment type")
}

Now imagine this logic repeated across multiple files.

Problems appear quickly:

  • Code duplication
  • Hard-to-maintain logic
  • Difficult testing
  • Violations of the Single Responsibility Principle

This is why developers rely on patterns that simplify complex object creation.

How the Factory Pattern Helps

The Factory Pattern solves these issues by:

  • Centralizing object creation
  • Reducing tight coupling
  • Making code easier to extend
  • Improving testability

Most importantly, it lets your business logic focus on what to do, not how objects are created.

That’s the real power behind Factory Pattern Simplify Complex Object Creation.

A Simple Kotlin Example

Define a Common Interface

Kotlin
interface Notification {
    fun send(message: String)
}

This interface represents a notification system.

Create Concrete Implementations

Kotlin
class EmailNotification : Notification {
    override fun send(message: String) {
        println("Sending Email: $message")
    }
}

class SmsNotification : Notification {
    override fun send(message: String) {
        println("Sending SMS: $message")
    }
}

class PushNotification : Notification {
    override fun send(message: String) {
        println("Sending Push Notification: $message")
    }
}

Each class has its own behavior but follows the same contract.

Create the Factory

Kotlin
object NotificationFactory {

    fun create(type: String): Notification {
        return when (type.uppercase()) {
            "EMAIL" -> EmailNotification()
            "SMS" -> SmsNotification()
            "PUSH" -> PushNotification()
            else -> throw IllegalArgumentException("Unknown notification type")
        }
    }
}

This is the heart of the Factory Pattern.

The factory:

  • Knows which object to create
  • Hides creation logic from the rest of the app

Use the Factory

Kotlin
val notification = NotificationFactory.create("EMAIL")
notification.send("Welcome to our platform!")

That’s it.

The calling code:

  • Does not know about concrete classes
  • Does not change if new notification types are added

This is how the Factory Pattern Simplify Complex Object Creation in Kotlin applications.

Why Kotlin Works So Well with Factory Pattern

Kotlin makes the Factory Pattern even cleaner because of:

  • object keyword for singletons
  • when expressions
  • Strong type safety
  • Concise syntax

Factories in Kotlin are:

  • Easy to read
  • Hard to misuse
  • Simple to test

This makes Kotlin a great fit for modern, scalable architecture.

Real-World Use Cases

You’ll see the Factory Pattern used in:

  • Database connection creation
  • Payment gateways
  • Logging frameworks
  • UI component generation
  • API client creation

Anywhere object creation depends on conditions, the Factory Pattern Simplify Complex Object Creation effectively.

Factory Pattern and Clean Architecture

From an architectural perspective, the Factory Pattern supports:

  • Loose coupling
  • Open/Closed Principle
  • Single Responsibility Principle

Your system becomes:

  • Easier to extend
  • Safer to modify
  • More readable for new developers

Common Mistakes to Avoid

Even with factories, mistakes happen.

Avoid:

  • Putting business logic inside the factory
  • Creating overly complex factories
  • Ignoring interfaces

A factory should only create objects, nothing more.

When Should You Use the Factory Pattern?

Use it when:

  • Object creation logic is complex
  • You expect future extensions
  • You want cleaner, testable code

Avoid it for:

  • Very small or one-off objects
  • Simple scripts

Conclusion

The Factory Pattern Simplify Complex Object Creation by separating object creation from object usage.

It keeps your code:

  • Clean
  • Flexible
  • Easy to maintain

In modern Kotlin applications, this pattern is not just useful. It’s often essential.

If you’re building scalable systems, learning and applying the Factory Pattern is a smart investment in code quality and long-term success.

Use Case Patterns

Mastering Use Case Patterns: A Practical Guide for Modern Software Design

Modern software design isn’t just about writing code that works; it’s about writing code that’s maintainable, scalable, and easy to understand. One way to achieve this is by using Use Case Patterns — a structured approach to modeling software functionality based on real-world user interactions. In this guide, we’ll break down everything you need to know about use case patterns, provide practical Kotlin examples, and show how to apply them effectively in your projects.

What Are Use Case Patterns?

A use case pattern is a reusable template that describes how a system should respond to specific user actions or events. Think of them as building blocks for designing your software’s functionality. Instead of starting from scratch each time, you can rely on patterns to standardize workflows, reduce errors, and speed up development.

For example, common use case patterns include:

  • Authentication — logging in and out
  • CRUD Operations — create, read, update, delete
  • Notification Handling — sending emails or push notifications

These patterns provide a blueprint for solving recurring problems, making your code more predictable and maintainable.

Why Use Use Case Patterns in Modern Software Design?

  1. Consistency: Patterns ensure that similar functionalities follow a consistent approach across your project.
  2. Reusability: Once you define a pattern, you can reuse it in multiple parts of your app without rewriting code.
  3. Clarity: Clear use case patterns make it easier for new developers to understand your system.
  4. Scalability: Patterns help design systems that can grow without becoming messy or unmanageable.

Core Principles of Use Case Patterns

To master use case patterns, keep these principles in mind:

  • Single Responsibility: Each pattern should handle one type of use case.
  • Clear Actors: Define who or what interacts with the system.
  • Explicit Steps: Document each step the system performs in response to an action.
  • Reusability: Design patterns so they can be applied in multiple scenarios.

Implementing Use Case Patterns in Kotlin

Kotlin is a modern, concise programming language that’s perfect for demonstrating use case patterns. Let’s go through a simple example: a user registration system.

Define the Use Case Interface

Start by creating an interface that represents the use case:

Kotlin
interface UseCase<in Input, out Output> {
    fun execute(input: Input): Output
}

Here’s what’s happening:

  • Input is the data the use case needs (e.g., user info).
  • Output is the result of executing the use case (e.g., success or error).
  • execute() is the method that contains the business logic.

Implement a Specific Use Case

Now, let’s implement a RegisterUserUseCase:

Kotlin
data class User(val username: String, val email: String, val password: String)

class RegisterUserUseCase : UseCase<User, Boolean> {
    override fun execute(input: User): Boolean {
        if (input.username.isEmpty() || input.email.isEmpty() || input.password.isEmpty()) {
            return false
        }
        println("User ${input.username} registered successfully!")
        return true
    }
}

Here,

  • The User data class holds user information.
  • RegisterUserUseCase implements the UseCase interface.
  • The execute method checks for valid input and prints a success message.
  • Returning true or false indicates whether the registration was successful.

Use the Use Case

Finally, use the pattern in your application:

Kotlin
fun main() {
    val registerUseCase = RegisterUserUseCase()
    val newUser = User("amol", "[email protected]", "password123")

    val isRegistered = registerUseCase.execute(newUser)
    println("Registration status: $isRegistered")
}

This simple example shows how use case patterns create a clear, reusable structure. You can now create other use cases, like LoginUserUseCase, following the same template.

Best Practices for Use Case Patterns

  1. Keep Use Cases Small: Avoid overloading a single use case with too many responsibilities.
  2. Focus on Business Logic: Use case patterns should contain only business logic — not UI or database code.
  3. Combine With Repositories: Use repositories or services for data access while keeping the use case focused.
  4. Document Clearly: Add descriptions for each use case to improve maintainability.

Advanced Tip: Chaining Use Cases

Sometimes, a single user action involves multiple steps. Kotlin’s flexibility allows chaining use cases:

Kotlin
class CompleteUserOnboardingUseCase(
    private val registerUserUseCase: RegisterUserUseCase,
    private val sendWelcomeEmailUseCase: SendWelcomeEmailUseCase
) : UseCase<User, Boolean> {
    override fun execute(input: User): Boolean {
        val registered = registerUserUseCase.execute(input)
        if (!registered) return false
        sendWelcomeEmailUseCase.execute(input)
        return true
    }
}

Here, the CompleteUserOnboardingUseCase combines registration and email notification, keeping each use case modular and reusable.

Conclusion

Mastering use case patterns is a game-changer for modern software design. They help you write cleaner, maintainable code that is easy to understand and scale. Using Kotlin, you can implement these patterns with minimal boilerplate, keeping your focus on business logic.

Start small, focus on clarity, and gradually build a library of reusable patterns. Before long, your software architecture will be robust, consistent, and much easier to maintain.

By embracing use case patterns, you not only improve your code today — you future-proof your projects for tomorrow.

Repository Pattern

How the Repository Pattern Makes Code Easier to Test, Maintain, and Scale

Software projects rarely stay small. Features grow, requirements change, and teams expand. When data access logic is tightly coupled with business logic, even a simple update can break multiple parts of the system.

This is where the Repository Pattern becomes extremely valuable.

In this blog, we’ll explain the Repository Pattern in a clear and beginner-friendly way using Kotlin examples. You’ll learn what it is, why it matters, and how it makes your code easier to test, maintain, and scale over time.

What Is the Repository Pattern?

The Repository Pattern is a design pattern that separates data access logic from business logic.

Instead of letting your services or view models talk directly to a database, API, or data source, all data operations are handled by a repository. Your business logic interacts only with the repository interface.

You can think of the repository as a middle layer that hides all the details of how data is stored or retrieved.

This separation leads to cleaner, safer, and more flexible code.

Why the Repository Pattern Is Important

Without the Repository Pattern, applications often suffer from:

  • Database queries scattered across the codebase
  • Business logic tightly tied to a specific database or framework
  • Difficult and slow unit testing
  • High risk when changing data sources

The Repository Pattern solves these problems by creating a single, consistent place for data access.

Core Structure of the Repository Pattern

A typical Repository Pattern implementation includes:

  1. A repository interface that defines allowed operations
  2. A repository implementation that handles actual data access
  3. Business logic that depends only on the interface

Let’s walk through a simple Kotlin example.

Repository Pattern in Kotlin

Step 1: Define a Data Model

Kotlin
data class User(
    val id: Int,
    val name: String
)

This is a simple data class that represents a user in the system.

Step 2: Create the Repository Interface

Kotlin
interface UserRepository {
    fun getById(id: Int): User?
    fun getAll(): List<User>
    fun add(user: User)
}

This interface defines what the application can do with user data. It does not care how or where the data is stored.

Step 3: Implement the Repository

Kotlin
class UserRepositoryImpl(private val database: UserDatabase) : UserRepository {

    override fun getById(id: Int): User? {
        return database.users.find { it.id == id }
    }

    override fun getAll(): List<User> {
        return database.users
    }

    override fun add(user: User) {
        database.users.add(user)
    }
}

This class contains all the data access logic. Whether the data comes from Room, SQL, an API, or another source, the rest of the app does not need to know.

Step 4: Use the Repository in Business Logic

Kotlin
class UserService(
    private val userRepository: UserRepository
) {
    fun registerUser(user: User) {
        userRepository.add(user)
    }
}

The service depends on the repository interface, not the implementation. This design choice is key to flexibility and testability.

How the Repository Pattern Improves Testability

Testing becomes much easier with the Repository Pattern because dependencies can be replaced with fake or mock implementations.

Fake Repository for Testing

Kotlin
class FakeUserRepository : UserRepository {
    private val users = mutableListOf<User>()

    override fun getById(id: Int): User? {
        return users.find { it.id == id }
    }

    override fun getAll(): List<User> {
        return users
    }

    override fun add(user: User) {
        users.add(user)
    }
}

You can now test your service without a real database:

Kotlin
val repository = FakeUserRepository()
val service = UserService(repository)

service.registerUser(User(1, "Amol"))

This approach results in faster, more reliable tests and supports accurate, verifiable behavior.

How the Repository Pattern Improves Maintainability

As applications grow, maintainability becomes more important than short-term speed.

The Repository Pattern helps by:

  • Keeping data logic in one place
  • Reducing duplicated queries
  • Making code easier to read and reason about
  • Allowing safe refactoring

If you need to update how users are stored or retrieved, you only change the repository implementation.

How the Repository Pattern Helps with Scalability

Scalability is about more than performance. It’s also about adapting to future changes.

With the Repository Pattern, you can:

  • Add caching inside the repository
  • Switch databases or APIs
  • Introduce pagination or background syncing

For example, you might later enhance this:

Kotlin
override fun getAll(): List<User> {
    return database.users
}

Without changing any business logic that depends on it.

Common Mistakes to Avoid

When using the Repository Pattern, avoid these pitfalls:

  • Putting business logic inside repositories
  • Exposing database-specific models directly
  • Adding unnecessary abstraction to very small projects

The Repository Pattern should simplify your code, not complicate it.

When Should You Use the Repository Pattern?

The Repository Pattern is a great choice when:

  • Your app has complex business rules
  • You expect data sources to evolve
  • You want clean unit tests
  • Your project is designed for long-term growth

For quick prototypes, it may be unnecessary. For production systems, it’s often worth the investment.

Conclusion

The Repository Pattern helps you write code that is easier to test, easier to maintain, and easier to scale.

By separating data access from business logic, you create a cleaner architecture that supports growth and change.

When implemented correctly in Kotlin, the Repository Pattern leads to reliable, readable, and future-proof applications that developers can trust.

@ApplicationContext

How @ApplicationContext Works in Jetpack Compose with Hilt : A Practical, Clean-Architecture Guide (With Runtime Explanation)

If you’ve ever used Hilt in a Jetpack Compose app, you’ve probably written code like this:

Kotlin
class MyRepository @Inject constructor(
    @ApplicationContext private val context: Context
)

And then paused for a second and thought:

“Okay… but where is this @ApplicationContext coming from?”
 
“Who creates it?”
 
“And how does Hilt magically inject it at runtime?”

This article answers those questions deeply and practically — without buzzwords, without hand-waving, and without unsafe patterns.

We’ll cover:

  • Why ViewModels should not receive Context
  • How Hilt resolves @ApplicationContext at runtime
  • The exact dependency flow from Compose → ViewModel → Repository
  • Why this approach is clean, safe, and testable
  • What code Hilt generates behind the scenes (conceptually)
  • Common mistakes and how to avoid them

Why Passing Context to ViewModel Is a Bad Idea

Let’s start with the mistake most Android developers make at least once:

Kotlin
class MyViewModel(private val context: Context) : ViewModel()

This looks harmless — until it isn’t.

Jetpack ViewModels are designed to outlive UI components like Activities and Fragments. An Activity context, however, is tied to the Activity lifecycle.

If a ViewModel holds an Activity context:

  • The Activity cannot be garbage collected
  • Memory leaks occur
  • Configuration changes become dangerous
  • Testing becomes harder

This is why Android’s architecture guidelines are very clear:

ViewModels should not hold a Context.

But what if you need access to:

  • SharedPreferences
  • DataStore
  • ConnectivityManager
  • Location services
  • File system APIs

You do need a Context — just not in the ViewModel. This is where repositories and Application context come in.

The Clean Architecture Rule

Here’s the mental model that solves this cleanly:

LayerResponsibilityContext Allowed
UI (Compose)Rendering, user inputYes (UI-only)
ViewModelState & business logicNo
RepositoryData & system accessYes
ApplicationApp lifecycleYes

So the rule is simple:
 If something needs a Context, it belongs below the ViewModel layer.

The Correct Dependency Flow

In a modern Compose app using Hilt, the flow looks like this:

Kotlin
Compose Screen

ViewModel (no Context)

Repository (Application Context)

Android System Services

The ViewModel never touches Context.
 The Repository owns it.
 The Application provides it.

So… Where Does @ApplicationContext Come From?

This is the part that feels like magic — but isn’t.

@HiltAndroidApp Creates the Root Component

Kotlin
@HiltAndroidApp
class MyApp : Application()

When you add this annotation, Hilt:

  • Generates a base class for your Application
  • Creates a singleton Application-level component
  • Stores the Application instance inside it

At runtime, Android creates your Application before anything else.

That Application instance is a Context.

Hilt Has a Built-In Context Provider

Inside Hilt’s internal codebase (not yours), there is a binding equivalent to:

Kotlin
@Provides
@Singleton
@ApplicationContext
fun provideApplicationContext(app: Application): Context = app

You never write this.
 You never import it.
 But it exists and is always available once @HiltAndroidApp is present.

So when Hilt sees:

Kotlin
@ApplicationContext Context

It knows exactly what to inject:
 ➡ the Application instance

Repository Requests the Context

Kotlin
class UserRepository @Inject constructor(
    @ApplicationContext private val context: Context
)

At compile time:

  • Hilt validates that a binding exists
  • It generates a factory class for MyRepository

Conceptually, the generated code looks like:

Kotlin
class MyRepository_Factory(
    private val contextProvider: Provider<Context>
) {
    fun get(): MyRepository {
        return MyRepository(contextProvider.get())
    }
}

At runtime:

  • contextProvider.get() returns the Application
  • The repository receives a safe, long-lived context

ViewModel Receives the Repository

Kotlin
@HiltViewModel
class UserViewModel @Inject constructor(
    private val repository: UserRepository
) : ViewModel()

The ViewModel:

  • Has no idea where the context comes from
  • Has no Android dependency
  • Is fully testable with fake repositories

Compose retrieves it like this:

Kotlin
val viewModel = hiltViewModel<UserViewModel>()

Hilt handles everything else.

A Real Example: SharedPreferences

Repository:

Kotlin
class UserPreferencesRepository @Inject constructor(
    @ApplicationContext context: Context
) {
    private val prefs =
        context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE)

    fun saveUsername(name: String) {
        prefs.edit().putString("username", name).apply()
    }

    fun loadUsername(): String =
        prefs.getString("username", "Guest") ?: "Guest"
}

The ViewModel remains clean:

Kotlin
@HiltViewModel
class UserViewModel @Inject constructor(
    private val repository: UserPreferencesRepository
) : ViewModel() {

    val username = MutableStateFlow("")

    fun load() {
        username.value = repository.loadUsername()
    }
}

Notice what’s missing?

No Context in the ViewModel.
 That’s the whole point.

Why This Is Safe (And Recommended)

Let’s address the usual concerns.

Will this leak memory?
 No. Application context lives as long as the app process.

Will this break on rotation?
 No. ViewModels are lifecycle-aware; repositories aren’t tied to UI.

Is this officially recommended?
 Yes. This matches Google’s own Compose + Hilt samples.

Is this future-proof?
 Yes. This is the architecture Android is moving toward, not away from.

What If You Forget @HiltAndroidApp?

Your app will crash early with a clear error:

Kotlin
Hilt Activity must be attached to an @HiltAndroidApp Application

This happens because:

  • No ApplicationComponent is created
  • No Context binding exists
  • Dependency graph cannot be resolved

This is Hilt protecting you — not failing silently.

The One Rule to Remember

Context belongs to the data layer, not the state layer.

If you follow this rule:

  • Your architecture scales
  • Your code stays testable
  • Your app avoids subtle lifecycle bugs

Conclusion

@ApplicationContext isn’t magic.
 It’s a well-defined dependency provided by Hilt at the Application level, injected safely into the data layer, and kept far away from your UI state.

Once you understand this flow, Compose + Hilt stops feeling mysterious — and starts feeling predictable.

If this helped you, consider sharing it with the next developer who asks:
 “But where does the Context come from?”

back and forward navigation

How to Add Back and Forward Navigation in Android Studio IDE

Navigating through code efficiently is crucial for productive Android development. Android Studio provides powerful back and forward navigation features that help you jump between different code locations, making it easier to trace code flow, review changes, and return to previous working contexts. In this comprehensive guide, we’ll explore everything you need to know about implementing and using back and forward navigation in Android Studio.

Understanding Navigation in Android Studio

Before diving into the implementation, it’s important to understand what back and forward navigation means in the context of an IDE. Unlike web browsers where you navigate between pages, in Android Studio, navigation refers to moving between different cursor positions in your code files. Every time you jump to a method definition, search for a usage, or click on a reference, Android Studio records that location in your navigation history.

Default Navigation Shortcuts

Android Studio comes with built-in keyboard shortcuts for back and forward navigation that work out of the box. These shortcuts vary depending on your operating system.

For Windows and Linux:

  • Navigate Back: Ctrl + Alt + Left Arrow
  • Navigate Forward: Ctrl + Alt + Right Arrow

For macOS:

  • Navigate Back: Cmd + [ or Cmd + Alt + Left Arrow
  • Navigate Forward: Cmd + ] or Cmd + Alt + Right Arrow

These shortcuts allow you to quickly move through your navigation history without taking your hands off the keyboard, significantly improving your coding workflow.

Using the Navigation Toolbar

If you prefer using the mouse or want visual confirmation of navigation actions, Android Studio provides toolbar buttons for back and forward navigation.

The navigation buttons are located in the main toolbar, typically near the top-left of the IDE window. They appear as left and right arrows, similar to browser navigation buttons. When you hover over these buttons, tooltips appear showing the keyboard shortcuts and the destination file or location.

To enable or customize the toolbar, go to View > Appearance > Toolbar to ensure the main toolbar is visible. 

If you don’t see the navigation arrows, you may need to customize your toolbar layout.

How Navigation History Works

Understanding how Android Studio tracks your navigation history helps you use these features more effectively. The IDE maintains a stack of cursor positions that gets updated when you perform certain actions.

Actions that add to navigation history include:

  • Jumping to a method or class definition using Ctrl + Click or Cmd + Click
  • Using “Go to Declaration” with Ctrl + B or Cmd + B
  • Finding usages with Alt + F7 or Cmd + F7
  • Navigating to a line number with Ctrl + G or Cmd + L
  • Using “Go to Class,” “Go to File,” or “Go to Symbol” navigation
  • Clicking on items in search results, find usages panels, or the structure view
  • Navigating through bookmarks

Actions that typically don’t add to navigation history:

  • Simple cursor movements with arrow keys
  • Scrolling through a file
  • Typing or editing code
  • Moving within the same visible screen area

This intelligent tracking ensures your navigation history remains useful and doesn’t get cluttered with every minor cursor movement.

Customizing Navigation Shortcuts

If the default shortcuts don’t suit your workflow or conflict with other tools, you can customize them to your preference.

To customize navigation shortcuts, navigate to File > Settings on Windows/Linux or Android Studio > Preferences on macOS. Then follow these steps:

First, expand the Keymap section in the left sidebar. You’ll see a search box at the top of the keymap panel. Type “navigate / navigate back” to find the back navigation action, and “navigate / navigate forward” for the forward navigation action.

Right-click on “Back” under the Navigation category and you’ll see options to add keyboard shortcuts, mouse shortcuts, or abbreviations. Select “Add Keyboard Shortcut” and press your desired key combination. 

Android Studio will warn you if the shortcut is already assigned to another action, allowing you to resolve conflicts.

Repeat the same process for “Forward” navigation. Once you’ve set your preferred shortcuts, click “Apply” and “OK” to save your changes.

Advanced Navigation Techniques

Beyond basic back and forward navigation, Android Studio offers several advanced navigation features that complement these basic functions.

Recent Files Navigation: Press Ctrl + E on Windows/Linux or Cmd + E on macOS to open a popup showing recently opened files. This provides a quick way to jump to files you’ve worked on recently without going through the full navigation history.

Recent Locations: Press Ctrl + Shift + E on Windows/Linux or Cmd + Shift + E on macOS to see recent cursor locations with code context. This shows you snippets of code from locations you’ve recently visited, making it easier to find the exact spot you’re looking for.

Bookmarks: Set bookmarks at important locations in your code with F11 to create an anonymous bookmark or Ctrl + F11 or Cmd + F11 to create a numbered bookmark. 

Navigate between bookmarks using Shift + F11 to see all bookmarks, providing persistent navigation points beyond your session history.

Navigate to Last Edit Location: Press Ctrl + Shift + Backspace on Windows/Linux or Cmd + Shift + Backspace on macOS to jump directly to the last place you made an edit. This is particularly useful when you’ve navigated away to check something and want to return to where you were actively coding.

Navigation in Split Editor Mode

When working with multiple editor windows in split mode, navigation becomes even more powerful. Android Studio maintains separate navigation histories for each editor pane, allowing you to navigate independently in different views.

To split your editor, right-click on a file tab and select “Split Right” or “Split Down.” You can then navigate through different parts of your codebase simultaneously. The back and forward navigation shortcuts will apply to whichever editor pane currently has focus.

This is particularly useful when you’re comparing implementations, refactoring code across multiple files, or working on related components simultaneously.

Best Practices for Efficient Navigation

To make the most of Android Studio’s navigation features, consider adopting these best practices in your daily workflow.

1. Learn and use keyboard shortcuts consistently rather than relying on mouse clicks. Muscle memory for navigation shortcuts can dramatically speed up your coding process. The time invested in learning these shortcuts pays dividends in increased productivity.

2. Combine navigation with other IDE features like code search, find usages, and structure view to create efficient navigation patterns. For example, use “Find Usages” to see all references to a method, click on one to examine it, then use back navigation to return to your starting point.

3. Use bookmarks strategically for code locations you return to frequently within a project. This creates persistent reference points that survive beyond your current session.

4. Take advantage of the “Recent Locations” feature when you need to review multiple code sections you’ve recently visited. This provides more context than simple back navigation by showing code snippets.

5. When refactoring or reviewing code, use forward navigation to retrace your steps after going back. This helps you verify that you’ve addressed all necessary changes in a logical sequence.

Troubleshooting Navigation Issues

Sometimes navigation features may not work as expected. Here are common issues and their solutions.

Navigation shortcuts not working: First, check if the shortcuts are being intercepted by your operating system or other applications. On Windows, some keyboard manager utilities might capture these key combinations. On macOS, check System Preferences for conflicting shortcuts. Verify your keymap settings in Android Studio to ensure the shortcuts are properly configured.

Navigation history seems incomplete: Navigation history has limits to prevent memory issues. If you’ve navigated through many locations, older entries may be dropped. Additionally, closing and reopening files or projects may reset certain navigation states. Consider using bookmarks for locations you need to preserve across sessions.

Navigation buttons missing from toolbar: Go to View > Appearance > Toolbar to ensure the toolbar is visible. If the toolbar is visible but navigation buttons are missing, try customizing or resetting your toolbar layout or updating Android Studio to the latest version.

Navigation jumps to unexpected locations: This can happen if files have been modified externally or if you’re working with generated code that gets refreshed. Ensure your project is properly synchronized with File > Sync Project with Gradle Files and that you’re not editing generated files that get overwritten.

Navigation in Large Projects

In large Android projects with hundreds or thousands of files, efficient navigation becomes even more critical. The combination of back/forward navigation with Android Studio’s other navigation tools creates a powerful workflow.

Use the project structure view in conjunction with navigation history. When you need to explore a new area of the codebase, use “Go to Class” or “Go to File” to jump to relevant files, examine them, and then use back navigation to return to your working context.

Leverage the “Call Hierarchy” feature with Ctrl + Alt + H or Cmd + Alt + H to understand method call chains. Navigate through the hierarchy, then use back navigation to return to your starting point. This combination helps you trace execution flow in complex applications.

The “Type Hierarchy” feature, accessed with Ctrl + H or Cmd + H, works similarly for understanding class inheritance and implementations. Navigate through the hierarchy tree, and use navigation history to backtrack when needed.

Conclusion

Mastering back and forward navigation in Android Studio is essential for efficient Android development. These features, combined with the IDE’s other navigation capabilities, create a powerful toolkit for exploring codebases, understanding code flow, and maintaining productivity.

Start by memorizing the basic keyboard shortcuts for your operating system, then gradually incorporate advanced navigation techniques into your workflow. As these patterns become second nature, you’ll find yourself navigating code with confidence and speed, spending less time searching for code and more time writing it.

Remember that effective navigation is about developing habits and patterns that work for your specific coding style. Experiment with different combinations of navigation features, customize shortcuts to match your preferences, and build a navigation workflow that maximizes your productivity in Android Studio.

AlarmManager

Understanding AlarmManager, PendingIntent, BroadcastReceiver, and BootReceiver in Android

Android background execution is one of the most misunderstood parts of the platform, especially when alarms, notifications, and device reboot come into play.

Most tutorials show what code to write. Very few explain why that code works, what Android is doing internally, or why certain architectural rules are non-negotiable.

This article fills that gap.

We’ll walk through AlarmManager, PendingIntent, BroadcastReceiver, and BootReceiver using real, production-style code, explaining not just the syntax but the system behavior behind it.

This is not a copy of documentation.
 It’s an experience-driven explanation based on how Android actually behaves in real apps.

The Core Problem Android Is Solving

Android applications do not run continuously.

The OS is designed to:

  • Preserve battery
  • Free memory aggressively
  • Kill background processes at any time

Yet apps still need to:

  • Show notifications at specific times
  • Perform actions when the app is closed
  • Restore scheduled tasks after a device reboot

Android solves this using system-controlled execution, not app-controlled execution.

That single idea explains everything else in this article.

AlarmManager: Scheduling Time, Not Code

The Most Common Misconception

“AlarmManager runs my code at a specific time.”

It doesn’t.

AlarmManager has one job only:

At a specific time, notify the Android system that something needs to happen.

AlarmManager:

  • Does not execute methods
  • Does not keep your app alive
  • Does not know what your app does

It simply stores:

  • A trigger time
  • A PendingIntent (what the system should do later)

Scheduling an Alarm (MainActivity)

Let’s look at real code and follow the execution flow.

Java
public class MainActivity extends AppCompatActivity {

    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        findViewById(R.id.btnSetAlarm).setOnClickListener(v -> scheduleAlarm());
        findViewById(R.id.btnCancelAlarm).setOnClickListener(v -> cancelAlarm());
    }

Creating the Intent

This Intent does not execute anything.
 It simply describes what component should be triggered later.

Java
private void scheduleAlarm() {
    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.setAction("com.softaai.alarmdemo.ALARM_ACTION");
    intent.putExtra("message", "Your scheduled reminder!");

PendingIntent: The Permission Slip for the System

Now comes the most important part.

Java
pendingIntent = PendingIntent.getBroadcast(
        this,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );

A PendingIntent tells Android:

“You are allowed to perform this action on my app’s behalf later, even if my app process no longer exists.”

Why this matters:

  • Your app may be killed
  • Your activity may never run again
  • Your app may not be in memory

Without PendingIntent, alarms would be impossible in a battery-safe OS.

Scheduling with AlarmManager

Now we calculate when the alarm should fire.

Java
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 30);

long triggerTime = calendar.getTimeInMillis();

And hand everything to the system:

Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(
            AlarmManager.RTC_WAKEUP,
            triggerTime,
            pendingIntent
        );
    } else {
        alarmManager.setExact(
            AlarmManager.RTC_WAKEUP,
            triggerTime,
            pendingIntent
        );
    }
}

Important:
 At this point, your app is no longer involved.
 AlarmManager stores the data and the system takes over.

What Actually Happens Internally

  1. AlarmManager stores the trigger time + PendingIntent
  2. Your app process can be killed at any moment
  3. When time arrives:
  • Android wakes up
  • Android fires the PendingIntent
  • Android decides how to re-enter your app

AlarmManager never runs your code.
 The system does.

BroadcastReceiver: The Entry Point Back Into Your App

When the alarm fires, Android needs a way to re-enter your app safely.

That entry point is a BroadcastReceiver.

Java
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
        if (message == null) message = "Time's up!";
        NotificationHelper.showNotification(
            context,
            "Alarm",
            message
        );
    }
}

What Android Does Here

  • Creates your app process if needed
  • Instantiates the receiver
  • Calls onReceive()
  • Expects it to finish quickly

Important constraints:

  • Runs on the main thread
  • Must finish in ~10 seconds
  • Not meant for long work

If you need long processing, hand off to WorkManager or a service.

Why Alarms Disappear After Reboot

This is intentional behavior.

When a device reboots:

  • RAM is wiped
  • System services restart
  • All alarms are cleared

Android does this to avoid restoring stale or invalid schedules.

So if your app uses alarms, you must restore them manually.

BootReceiver: Restoring Alarms After Reboot

This is where BootReceiver comes in.

Java
public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            rescheduleAlarms(context);
        }
    }

Rescheduling the Alarm

Java
private void rescheduleAlarms(Context context) {
    AlarmManager alarmManager =
        (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent alarmIntent = new Intent(context, AlarmReceiver.class);
    alarmIntent.setAction("com.softaai.alarmdemo.ALARM_ACTION");
    alarmIntent.putExtra("message", "Alarm restored after reboot");
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
        context,
        0,
        alarmIntent,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );
    
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, 1);
    alarmManager.setExactAndAllowWhileIdle(
        AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(),
        pendingIntent
    );
}

A BootReceiver exists for one reason only:

Re-create state lost due to reboot.

Nothing more.

Why AlarmReceiver and BootReceiver Should Be Separate

1. Different Responsibilities

  • AlarmReceiver → time-based events
  • BootReceiver → system lifecycle events

Mixing them creates confusing and fragile code.

2. Different Security Requirements

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
XML
<receiver
    android:name=".BootReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<receiver
    android:name=".AlarmReceiver"
    android:exported="false" />

Boot receivers must be exported.
 Alarm receivers should not be.

Combining them exposes alarm logic to other apps. That’s a security bug.

3. Boot Is a Fragile System State

During boot:

  • Network may be unavailable
  • Storage may not be ready
  • UI work is unsafe

Separating receivers prevents accidental crashes and ANRs.

Industry-Standard Architecture

Well-designed Android apps follow this pattern:

  • AlarmReceiver → reacts to alarms
  • BootReceiver → restores alarms
  • Shared logic → scheduler/helper classes

Receivers stay thin.
 Business logic stays reusable.

This is how production Android apps are built.

Key Takeaways

  • AlarmManager schedules time, not code
  • PendingIntent is a system-approved execution token
  • BroadcastReceiver is a system entry point, not a worker
  • Alarms are wiped on reboot by design
  • BootReceiver restores lost state
  • Combining receivers is unsafe and unmaintainable

FAQ 

Does AlarmManager run my code?

No. It only notifies the system, which decides when and how to re-enter your app.

Why is PendingIntent required?

It allows Android to safely execute actions even if your app process is dead.

Why do alarms disappear after reboot?

Android clears all alarms intentionally to avoid restoring invalid schedules.

Is BootReceiver mandatory for alarm apps?

Yes. Without it, alarms will silently stop after reboot.

Conclusion

Android background execution is not about forcing your app to stay alive.

It’s about cooperating with the system so your app runs only when it is allowed, necessary, and safe.

Once you understand that mindset, AlarmManager, PendingIntent, and BroadcastReceivers stop feeling magical — and start feeling predictable.

How to Fix Wrong Git Commits in Android Studio and Safely Remove Unwanted Remote Commits

How to Fix Wrong Git Commits in Android Studio and Safely Remove Unwanted Remote Commits

If you’ve ever opened GitHub and noticed the wrong name on your commits — or spotted a commit on your main branch that never should’ve been there. These are some of the most common (and stressful) Git problems developers run into, especially when working in teams or switching between multiple accounts.

The good news is this: Git gives you the tools to fix both issues cleanly. The bad news? Using the wrong command can make things worse if you don’t understand what’s really going on.

This guide walks you through:

  • Why Android Studio shows the wrong Git user
  • How to correctly change the Git author (without breaking anything)
  • How to fix commits that are already pushed
  • When to remove commits entirely vs safely reverting them
  • Best practices to avoid these problems in the future

Everything here is based on how Git actually works under the hood — not IDE myths.

Why Android Studio Shows the Wrong Git User

Let’s clear up the most common misunderstanding first:

Android Studio does not control your Git commit author. Git does.

Android Studio is just a client. When you click “Commit,” it asks Git two questions:

  • What is the author name?
  • What is the author email?

Git answers based on its configuration files. That’s it.

So changing your GitHub account inside Android Studio affects authentication (push and pull permissions), but it does not change the commit author. That’s why this issue keeps coming back.

How Git Decides Who You Are

Git identifies authors using two values:

  • user.name
  • user.email

These can exist at two levels:

  1. Global — applies to all repositories on your machine
  2. Local (project-specific) — applies only to the current repository

Git always prefers local settings over global ones.

The Correct Way to Change Git User in Android Studio (Recommended)

If you work on multiple projects or use more than one GitHub account, this is the safest approach.

Step 1: Open the Terminal in Android Studio

Open your project, then click Terminal at the bottom.

Step 2: Set the Git user for this project only

Bash
git config user.name "Your Correct Name"<br>git config user.email "[email protected]"

Step 3: Verify

Bash
git config user.name<br>git config user.email

From this point forward, all new commits in this project will use the correct author.

Changing Git User Globally (Use With Caution)

If every repository on your machine is using the wrong user, update the global config:

Bash
git config --global user.name "Your Name"<br>git config --global user.email "[email protected]"

Verify with:

Bash
git config --global --lista

This affects all Git repositories on your system.

Why Switching GitHub Accounts Doesn’t Fix Commit Names

Android Studio’s GitHub settings only control:

  • Authentication
  • Push and pull permissions

They do not control commit authorship. That’s why you can push to one account while commits show another name entirely.

SSH Keys: Why Pushes Go to the Wrong Account

If you’re using SSH, GitHub identifies you by your SSH key, not your username.

Check which account your machine is using:

If GitHub responds with the wrong username, your SSH key is attached to the wrong account.

The Correct Fix

  • Generate a new SSH key
  • Add it to the correct GitHub account
  • Configure ~/.ssh/config to explicitly use that key

This ensures commits and permissions align correctly.

Why Existing Commits Don’t Update Automatically

Changing Git config only affects future commits.

Once a commit exists:

  • Its author is permanent
  • It cannot be changed unless history is rewritten

That’s intentional. Git values integrity over convenience.

How to Fix the Author of the Last Commit

If the most recent commit has the wrong author:

Bash
git commit --amend --author="Your Name <[email protected]>"<br>git push --force-with-lease

Only do this if rewriting history is acceptable.

Fixing Multiple Commits With the Wrong Author

Use interactive rebase:

Bash
git rebase -i HEAD~N

Change pick to edit for the commits you want to fix, then run:

Bash
git commit --amend --author="Your Name <[email protected]>"<br>git rebase --continue

Finish with:

Bash
git push --force-with-lease

Removing an Unwanted Commit From a Remote Repository

This is where many developers panic. The right solution depends on who else is affected.

Option 1: Hard Reset (Deletes History)

Use this only if:

  • It’s your personal branch, or
  • The commit contains sensitive data

Steps

Bash
git log --oneline<br>git reset --hard <last-good-commit><br>git push origin <branch> --force-with-lease

Everyone else must reset their local branch afterward.

Option 2: Revert (Safest for Teams)

If the branch is shared or protected, always revert.

Bash
git revert <bad-commit-hash><br>git push origin <branch>

This creates a new commit that undoes the change without rewriting history.

When to Use Reset vs Revert

SituationRecommended
Secrets pushedReset + force push
Mistake on mainRevert
Cleaning your feature branchReset
Team already pulledRevert

Removing Multiple Commits Cleanly (Interactive Rebase)

For mixed good and bad commits:

Bash
git rebase -i HEAD~5

Change pick to drop for unwanted commits, then:

Bash
git push --force-with-lease

Always create a backup branch first:

git branch backup-before-cleanup

Common Mistakes to Avoid

  • Force-pushing without warning your team
  • Using --force instead of --force-with-lease
  • Rewriting history on protected branches
  • Forgetting to back up before rebasing

If something goes wrong, git reflog can usually save you.

Best Practices to Prevent These Issues

  • Always verify Git user before starting a project
  • Prefer project-level Git config
  • Use separate SSH keys for different accounts
  • Protect main branches with PR rules
  • Never force-push without coordination
  • Keep commit emails matching your GitHub account

Conclusion

Android Studio often gets blamed for Git problems it doesn’t actually control. Once you understand that Git owns identity and history, everything becomes easier to reason about — and fix.

By setting the correct Git user, managing SSH keys properly, and choosing the right strategy for removing commits, you can keep your repositories clean without disrupting your team.

If you treat Git history with intention instead of panic, it becomes a powerful tool instead of a constant source of stress.

DirectoryLock Process Still Running

Fixing the “DirectoryLock / Process Still Running” Error in Android Studio Across Windows, macOS, and Linux

If Android Studio refuses to start and throws an error like:

Bash
Internal error<br>com.intellij.platform.ide.bootstrap.DirectoryLock$CannotActivateException: Process "studio64.exe" is still running and does not respond.

…you’re dealing with one of the most common (and confusing) Android Studio startup issues.

It usually appears after a crash, a forced shutdown, or reopening the IDE too quickly. The good news is that this error does not mean your installation is broken. It’s a safety mechanism that fails to recover properly in real-world conditions.

This guide explains what’s actually happening under the hood, why it shows up more often on Windows, how it behaves on macOS and Linux, and how to fix — and prevent — it permanently.

What This Error Really Means

Android Studio is built on the JetBrains IntelliJ Platform, which enforces a single-instance lock.

When Android Studio starts, it does three important things:

  1. Creates a lock file in its system directory
  2. Binds to a local socket (internal communication port)
  3. Registers itself as the active IDE instance

When you close Android Studio normally, all of that is cleaned up.

But if the IDE:

  • crashes,
  • is force-killed,
  • is interrupted by sleep or shutdown,
  • or hangs during indexing or Gradle sync,

those locks don’t always get released.

When you try to reopen Android Studio, it checks for those locks, assumes another instance is still running, and blocks startup to protect your data.

That’s when you see errors like:

  • DirectoryLock$CannotActivateException
  • Process is still running and does not respond
  • Address already in use
  • Connection refused

This behavior is intentional — but the recovery is imperfect.

Is this a particular OS problem?

No.
 This error is cross-platform and occurs on Windows, macOS, and Linux.

That said, Windows developers see it far more often, and there are clear technical reasons for that.

Why Windows Is Affected the Most

1. Fast Startup (Hybrid Shutdown) — Windows Fast Startup doesn’t fully terminate background processes. If Android Studio was open during shutdown, its locks may survive the reboot.

2. Aggressive File Locking — Windows uses mandatory file locks. If the IDE freezes or crashes, those locks are more likely to be left behind.

3. Antivirus & Defender Interference — Real-time scanners can delay or block the IDE’s socket binding, triggering false “already running” states.

4. Zombie Java Processes — If studio64.exe or its Java runtime crashes, Windows may leave it running invisibly in the background.

Result:
 Windows users encounter this error far more frequently than macOS or Linux users.

macOS: Less Common, Still Possible

macOS handles process cleanup more gracefully, which reduces the frequency — but doesn’t eliminate it.

Common macOS triggers include:

  • Force-quitting Android Studio
  • System sleep or sudden power loss
  • Corrupted IDE cache after a crash
  • Multiple user sessions sharing the same home directory

Because macOS is Unix-based, socket cleanup is usually reliable, but stale lock files can still remain after abnormal exits.

Linux: Rare, But Not Impossible

Linux is the least affected platform.

When the error appears, it’s usually caused by:

  • Killing the IDE with kill -9
  • Running Android Studio under different users
  • Permission issues in .config or .cache
  • Snap or Flatpak sandbox limitations

Linux aggressively cleans up sockets and file locks, which is why this issue is relatively rare there.

Real-World OS Comparison

Operating SystemLikelihoodOverall Stability
WindowsHighMost problematic
macOSMediumMostly stable
LinuxLowMost reliable

Step-by-Step Fix (Works on Any OS)

1. Kill the Stuck Process (Most Common Fix)

Windows

  • Open Task Manager → Details
  • End studio64.exe or java.exe

macOS

  • Open Activity Monitor
  • Force quit Android Studio or Java

Linux

Bash
pkill -f android-studio

If the error shows a PID, target that process directly.

2. Delete Leftover Lock Files

Sometimes the process is gone, but the lock file remains.

Windows

Bash
C:\Users\<User>\AppData\Local\Google\AndroidStudio<version>/

macOS

Bash
~/Library/Caches/Google/AndroidStudio<version>/

Linux

Bash
~/.cache/Google/AndroidStudio<version>/

Delete files such as:

  • port.lock
  • .lock
  • any *.lock files

Then restart Android Studio.

3. “Address Already in Use” Errors

If you see BindException or Address already in use, the fastest fix is a full system reboot.
 This clears all sockets and zombie processes instantly.

Why Android Studio Is Designed This Way

This behavior isn’t unique to Android Studio.

All JetBrains IDEs (IntelliJ IDEA, PyCharm, WebStorm) use the same architecture. The IDE prioritizes data safety over convenience:

  • It blocks startup if another instance is detected
  • It prevents concurrent access to system directories
  • It assumes clean shutdowns

The problem isn’t the design — it’s that real machines crash, sleep, and lose power.

How to Prevent This Issue Long-Term

Best Practices (All OS)

  • Always close Android Studio normally
  • Avoid force-killing unless absolutely necessary
  • Wait a few seconds before reopening after closing
  • Keep only one instance per project
  • Stay on the latest stable Android Studio release

Windows-Specific Tips

  • Disable Fast Startup
  • Add Android Studio folders to antivirus exclusions
  • Clean IDE cache directories occasionally

macOS & Linux Tips

  • Avoid force-quit
  • Check directory permissions
  • Don’t mix system installs with Snap/Flatpak
  • Kill leftover processes before relaunching

Conclusion

This error is not your fault, and it’s not a sign of a broken setup.

It happens because of:

  • How Android Studio manages single-instance locks
  • How operating systems handle crashes and shutdowns
  • Edge cases the IDE still doesn’t recover from perfectly

Once you understand what’s going on, fixing it takes minutes — and preventing it becomes easy.

If you work with Android Studio long enough, you’ll see this error at least once. Now you know exactly what to do when it happens.

Adding Images to GitHub Gists

Adding Images to GitHub Gists: What Works, What Doesn’t, and Why

GitHub Gists are great for sharing small pieces of code, configuration files, or quick notes. They’re fast, lightweight, and easy to link.

But many developers hit the same question sooner or later:

How do we add images to GitHub Gists?

If you’ve tried uploading an image directly or embedding one like you would on GitHub README files, you’ve probably noticed it’s not straightforward.

In this guide, we’ll walk through adding Images to GitHub Gists in detail. You’ll learn what works, what doesn’t, and why GitHub behaves the way it does. We’ll also cover reliable methods with clear examples you can use right away.

Why Adding Images to GitHub Gists Is Confusing

GitHub Gists look similar to repositories, but they’re not the same thing.

A few key differences matter here:

  • Gists are designed for small, single-purpose snippets
  • They don’t have a full file browser like repos
  • They don’t support direct image uploads through the UI

Because of this, adding Images to GitHub Gists require a different approach.

Can We Upload Images Directly to a GitHub Gist?

Short answer: No.

GitHub Gists do not support direct image uploads the way repositories do.

You can:

  • Paste text files
  • Add Markdown
  • Add code files

But you cannot:

  • Upload PNG, JPG, or GIF files directly
  • Drag and drop images into a Gist

That’s not a bug. It’s a design choice.

The Correct Way to Add Images to GitHub Gists

Even though you can’t upload images directly, you can display images in a Gist using external image hosting.

The idea is simple:

  1. Host the image somewhere else
  2. Embed it using Markdown

Let’s go through the working methods.

Method 1: Using GitHub Repository Images (Most Reliable)

This is the best and most stable way to add Images to GitHub Gists.

Step 1: Upload the Image to a GitHub Repository

Create a repository or use an existing one, then upload your image:

Markdown
my-repo/
└── images/
    └── img1.png

Once uploaded, click the image and copy the raw URL.

It will look like this:

Markdown
https://raw.githubusercontent.com/username/repo/main/images/img1.png
Step 2: Embed the Image in Your Gist

Use standard Markdown syntax inside your Gist:

Markdown
![Example Image1](https://raw.githubusercontent.com/username/repo/main/images/img1.png)
  • ! tells Markdown this is an image
  • Example Image1 is the alt text (important for accessibility and SEO)
  • The URL points to the raw image file

This method works consistently and is trusted by GitHub.

Method 2: Using GitHub Issues or Comments as Image Hosts

This method is popular but less controlled.

How It Works
  1. Open any GitHub issue or discussion
  2. Drag and drop your image into the comment box
  3. GitHub uploads the image and generates a CDN URL

The URL will look like this:

Markdown
https://user-images.githubusercontent.com/12345678/img2.png
Embed It in Your Gist
Markdown
![Screenshot](https://user-images.githubusercontent.com/12345678/img2.png)
Pros and Cons

Pros

  • Quick and easy
  • No extra repo needed

Cons

  • Image ownership is unclear
  • Harder to manage long-term
  • Not ideal for documentation that must last

For short-lived examples, this approach works. For professional use, prefer repositories.

Method 3: Using External Image Hosting Services

You can also use services like:

  • Imgur
  • Cloudinary
  • Your own server
Markdown
![Flow Diagram](https://example-cdn.com/images/flow.png)

Important Notes

  • Make sure the image URL is public
  • Avoid services that block hotlinking
  • Prefer HTTPS for security

This method works, but reliability depends on the host.

What Does NOT Work (Common Mistakes)

Understanding what doesn’t work is just as important.

1. Relative Paths

This will not work in Gists:

Markdown
![Image](./image.png)

Why?
Because Gists don’t have a file system like repositories.

2. HTML <img> Tags with Local Files

This also fails:

Markdown
<img src="image.png" />

The browser has no idea where image.png lives.

3. Dragging Images into Gists

You can drag images into GitHub issues, but not into Gists.

If you try, nothing happens.

Why GitHub Designed Gists This Way

GitHub Gists are meant to be:

  • Lightweight
  • Fast
  • Focused on code

Allowing image uploads would:

  • Increase storage costs
  • Complicate versioning
  • Move Gists away from their core purpose

That’s why adding Images to GitHub Gists rely on external hosting.

Best Practices for Adding Images to GitHub Gists

To keep your Gists clean and professional, follow these tips:

Use Descriptive Alt Text

Instead of:

Markdown
![img](url)

Use:

Markdown
![API response structure diagram](url)

This improves:

  • Accessibility
  • Search visibility
  • AI answer extraction

Keep Images Small and Relevant

Large images slow down loading and distract from the code.

Ask yourself:

  • Does this image explain something better than text?
  • Is it necessary?

If yes, include it. If not, skip it.

Version Your Images

If your image changes over time:

  • Store it in a repo
  • Update filenames or folders

This avoids broken references in old Gists.

Conclusion

Adding Images to GitHub Gists isn’t hard once you understand the rules.

You can’t upload images directly, but you can embed them reliably using external URLs. GitHub repositories are the safest option, while issue uploads and external hosts work in specific cases.

Use images sparingly, explain them clearly, and your Gists will be far more useful than plain code alone.

error: Content is protected !!