tezvyn:

Android App Startup: From Cold to Hot

AI-drafted, machine-checkedSource: developer.android.comintermediate
Android App Startup: From Cold to Hot

App startup has three types: cold, warm, and hot. Optimization focuses on the cold start—when the app launches from scratch. This is critical for user retention, as slow launches are a common reason for uninstalls. The footgun: don't just test warm starts.

WHY IT EXISTS: Users expect apps to be instant. A slow launch is the first impression an app makes, and a bad one leads to frustration and uninstalls. The Android OS needs to create a process, load the app's code, create UI, and perform the first draw, all of which takes time and must be actively optimized.

THE MENTAL MODEL: App startup isn't one thing; it's three distinct states. A COLD START is when the app's process doesn't exist in memory; the system builds everything from scratch. This is the slowest and most important to optimize. A WARM START is when the process exists, but the main activity must be recreated. A HOT START is when the app is already running in the background and is simply brought to the foreground. Your main enemy is the cold start.

HOW IT WORKS: During a cold start, the system performs a sequence of tasks: it creates a new process, loads the Application class and calls its onCreate(), launches the main thread, creates the main activity, inflates layouts, and performs the first draw. Each step is a potential bottleneck. Common culprits are heavy initialization in the Application class, complex view hierarchies that are slow to inflate, or synchronous I/O (disk or network) on the main thread. These blocking operations prevent the UI from being drawn, leaving the user staring at a blank screen.

WHEN TO USE IT: Startup optimization is crucial for any user-facing Android app. It's especially important for apps with large dependencies, complex initial screens, or those that need to perform network requests or database access at launch. Use tools like the Android Studio Profiler and Macrobenchmark library to measure startup time accurately and identify bottlenecks before you start changing code.

WHEN NOT TO USE IT: Don't prematurely optimize. Profile first. If your cold startup is already under 500ms, your time is better spent elsewhere. Shaving 50ms off a fast startup is less impactful than fixing a 5-second startup caused by a single blocking network call. Focus on the biggest, most obvious bottlenecks revealed by your profiling tools.

ONE CANONICAL EXAMPLE: A common optimization is to move heavy initialization off the main thread. Instead of initializing a heavy analytics SDK or a complex object graph synchronously in your Application's onCreate(), you can use a library like the App Startup library to control initialization order and move non-essential tasks to a background thread. For dependency injection, you can inject a Lazy<MyManager> instead of MyManager directly, which defers the object's creation until it's actually used for the first time.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.