How to handle screen orientation change when progress dialog and background thread active?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Handling screen orientation changes in Android can be challenging, especially when dealing with background operations and progress dialogs. This operation involves maintaining a consistent user experience without interrupting ongoing tasks or losing user data. This article will explore strategies to manage screen orientation changes effectively while a background thread is active, and a progress dialog is being shown.
Understanding the Problem
When the screen orientation changes, Android typically destroys and recreates the activity. This means any long-running tasks, UI elements, or dialogs may be lost unless explicitly handled. We need a strategy to manage these changes without disrupting the user experience.
Strategies for Handling Screen Orientation Changes
1. Preserve State with ViewModel
The ViewModel class is part of Android Architecture Components and provides a way to manage UI-related data in a lifecycle-conscious manner. By utilizing ViewModel, you can store data related to the background operations that persist through configuration changes.
Implementation
- Create a ViewModel:
- Use ViewModel in Activity:
2. Use Retain Instance in Fragments
For dialog-related tasks, a Fragment that retains its instance may be useful. By setting a fragment to retain its instance, you ensure that it will not be destroyed and recreated during configuration changes.
Implementation
- Create a Retainable Fragment:
3. Utilize onSaveInstanceState
Store essential information in the Bundle within onSaveInstanceState method. Although this will not keep threads running in the background, it will ensure essential data is not lost.
Implementation
4. Use Service or WorkManager for Long-Running Tasks
For tasks that must persist beyond the activity lifecycle, a Service or WorkManager is advisable. A Service runs in the background without a UI, and a WorkManager is a flexible API for executing deferrable, guaranteed background work.
Example with WorkManager:
5. Leverage Configuration Change Callbacks
Override onConfigurationChanged in your activity to directly manage specific attributes during orientation changes while avoiding a complete activity restart.
Implementation
Summary Table
| Strategy | Description | Appropriate Use Cases |
| ViewModel | Retains data across configurations but not background threads | Storing and observing UI-related data |
| Retain Instance in Fragments | Retains fragments and dialogs across configuration changes | Managing dialogs without re-creation |
| onSaveInstanceState | Saves essential UI states before destruction | When minimal data persistence is required |
| Service/WorkManager | Executes tasks that must persist beyond activity life | Long-running background tasks, independent of UI |
| onConfigurationChanged Callback | Manages certain aspects on configuration changes | When a specific aspect needs customization |
Conclusion
Handling screen orientation changes while maintaining an active background thread and progress dialog requires a multifaceted approach combining architectural components like ViewModel, retained fragments, and background services. By choosing the appropriate strategy based on your app's needs, you can create a seamless user experience that gracefully handles these changes.

