Posts

No More DisposableEffect Hacks: The Magic of Modifier.keepScreenOn()

Image
  How Jetpack Compose 1. 9+ finally simplified screen-awake logic with a single, lifecycle-aware modifier. For years, Android developers have shared a collective minor headache: keeping the screen awake. Whether for a navigation app, a recipe guide, or a media player, we’ve had to wrestle with the imperative  Window  API. With the arrival of newer versions of  Jetpack Compose (around 1.9+) , the struggle for  Android keep screen awake  scenarios is finally over. Enter  Modifier.keepScreenOn() —the simple API that brings a massive quality-of-life upgrade to the declarative world. The “Old Way”: A Ritual of Manual Management Before this update, preventing a device from dimming required stepping out of the Composable world and back into the Activity lifecycle. This approach was error-prone and easy to misuse, especially when it came to ensuring the flag was cleared during lifecycle transitions. 📊 Old vs. New: At a Glance Press enter or click to view ima...

🚀 Why You Should Know ArrayDeque in Kotlin (and Android)

Image
 Stop using LinkedList. Master the high-performance circular buffer for efficient Queue and Stack operations. If you are still reaching for  MutableList  or the classic  LinkedList  for every queue-like operation, it’s time for an upgrade. Meet  ArrayDeque , the high-performance powerhouse of the Kotlin Collections API. While  ArrayList  is the king of random access, it struggles when you need to manipulate the "head" of the list.  ArrayDeque  (Double-Ended Queue) fills this gap, offering a versatile, efficient, and Kotlin-idiomatic way to manage data from both ends. 🧠 The Tech Behind the Speed: Circular Arrays Unlike a  LinkedList , which creates a new "Node" object for every single element—leading to higher memory overhead and poor CPU cache locality— ArrayDeque  is backed by a  resizable circular array . Because it uses circular logic, it doesn’t need to “shift” every other element when you add something to the front. ...

Android Threading Internals: Mastering Handler, Looper & MessageQueue (With Real ANR Fix)

Image
 A deep dive into the core OS components that power smooth UI and prevent ANRs. Have you ever wondered how the Android OS manages complex multi-threading within its core components? While modern developers often reach for Coroutines or WorkManager, the  core execution engine  under the hood is a trio of classes:  Looper ,  Handler , and  MessageQueue . Understanding this system is the key to mastering Android performance. If the Main Looper is blocked, it cannot process input events or draw the UI, leading directly to the dreaded  ANR (Application Not Responding)  error. The Architecture: A Temporal Post Office To visualize how these work, think of a persistent worker thread as a high-efficiency post office: The MessageQueue (The Mailbox):  A repository for tasks (Messages or Runnables). It processes tasks based on  execution time . Internally, it uses native polling (via Linux  epoll ) to wait for new messages without wasting CPU c...