Android Only the original thread that created a view hierarchy can touch its views.
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android development, a common rule developers encounter is: "Only the original thread that created a view hierarchy can touch its views." This principle is central to the design of the Android UI framework, where the main thread, also known as the UI thread, is responsible for managing all UI operations. Understanding this rule is crucial for building responsive and crash-free Android applications.
The Main Thread
The main thread is where Android applications run by default. It is responsible for processing events, updating UI controls, and executing lifecycle callbacks for activities and fragments. The Android operating system enforces strict single-threaded access to UI operations to prevent concurrency issues. Such issues can lead to inconsistent UI states, application crashes, or difficult-to-debug threading issues.
Why UI Operations Are Limited to the Main Thread
- Concurrency Control: Allowing multiple threads to modify UI elements could lead to race conditions where the threads serve competing requests at the same time.
- Avoiding Complex Synchronizations: Without this limitation, developers would need to implement complex synchronization mechanisms like locks, which could lead to deadlocks or performance issues.
- Simplicity: Limiting UI updates to the main thread streamlines the UI programming model for developers, as they do not need to concern themselves with the coordination between multiple threads for UI rendering.
Violating the Rule
If a background thread attempts to modify a view, the app will throw an android.view.ViewRootImpl$CalledFromWrongThreadException with a message stating that only the original thread that created the view hierarchy can touch its views. Consider the following example:
Correct Handling With Handler
The most common way to update UI from a background thread is by using an Android Handler. Handler associated with the main thread can post Runnable tasks to the main thread's message queue. Here's how you can safely update a TextView from a background thread:
Using AsyncTask
AsyncTask was traditionally used to perform operations in the background and publish results on the UI thread. While it is deprecated as of Android 11, it’s important to understand it for older codebases:
Using Other Concurrency Frameworks
- Java
ExecutorService: Allows managing a pool of threads and is suitable for complex operations. - RxJava: Enables reactive programming patterns, simplifying threading with observable streams and schedulers.
- Kotlin Coroutines: Provides an expressive way to handle concurrency, leveraging syntax like
launchandasyncto manage background work efficiently.
Table Summary: Key Points
| Aspect | Details |
| Main Thread | Default thread for UI components |
| Rule Enforcement | Prevents race conditions and synchronization complexity |
| Common Violation Exception | ViewRootImpl$CalledFromWrongThreadException |
| UI Thread Updaters | Handler, AsyncTask (deprecated), ExecutorService, etc. |
| Modern Alternatives | RxJava, Kotlin Coroutines |
Conclusion
The rule "Only the original thread that created a view hierarchy can touch its views" is a fundamental aspect of Android development, ensuring that applications remain stable and responsive. Although this may seem restrictive, Android offers various mechanisms to safely update the UI from background threads. Understanding and applying these techniques is pivotal for developing efficient and reliable Android applications.

