Prevent screen rotation on Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Preventing screen rotation on Android is usually done either in the manifest or at runtime from an Activity. The best option depends on whether the screen should always stay fixed or only stay fixed in certain situations. The technical part is simple. The real design question is whether locking orientation improves the experience or just hides layout problems.
Lock Orientation in the Manifest
If an activity should always stay in one orientation, declare it in AndroidManifest.xml:
Common values include:
- '
portrait' - '
landscape' - '
sensorPortrait' - '
sensorLandscape'
For a hard lock, portrait or landscape is the usual choice.
This is the simplest solution because it applies before the activity is shown and does not require runtime code.
Lock Orientation at Runtime
If you only want to prevent rotation in certain states, set the requested orientation in code.
In Kotlin:
In Java:
This is useful when orientation should change based on feature state, user action, or device mode.
Choose Manifest Versus Runtime Intentionally
Use the manifest when:
- the activity should always stay fixed
- the lock is part of the screen's permanent design
- you want the simplest configuration
Use runtime locking when:
- the orientation changes based on app state
- some screens or modes should rotate and others should not
- the lock should be toggled dynamically
That distinction keeps the code cleaner. Permanent behavior belongs in configuration. Conditional behavior belongs in code.
Avoid Confusing Orientation Lock with Configuration Handling
Some developers try to "prevent rotation" by handling configChanges manually. That is a different topic.
This:
does not lock the screen by itself. It only changes how the activity reacts when a configuration change occurs.
If the goal is simply "do not rotate," screenOrientation or requestedOrientation is the direct solution.
Think About UX Before Locking
Orientation locks are sometimes necessary, but they are not always a good default.
Locking can make sense for:
- camera flows
- games designed for one orientation
- forms or flows that break badly in landscape
But it can be a poor choice for:
- content-reading apps
- video or media-heavy screens
- large-screen and tablet layouts
A forced portrait layout can feel restrictive on tablets or foldables if the UI could reasonably adapt instead.
Rotation Can Reveal State Bugs
Many apps lock orientation because recreation on rotation exposes weak state handling. That fixes the symptom, not the architecture.
If your UI loses data or resets incorrectly during rotation, the real fix may be:
- saving UI state properly
- using
ViewModel - making the layout responsive
Orientation locking is fine when it is a deliberate product decision. It is a poor substitute for state management.
Unlocking Again at Runtime
If you set orientation dynamically and later want to allow user-driven rotation again, use a less restrictive requested orientation:
That returns control to the normal system behavior.
This is useful in workflows such as:
- lock portrait during data entry
- unlock during media playback
The key is to make the transitions intentional and easy to reason about.
Common Pitfalls
The biggest mistake is using configChanges and assuming it prevents rotation. It does not.
Another issue is locking orientation to avoid dealing with state restoration bugs. That may make the app feel rigid without solving the real lifecycle problem.
Developers also sometimes apply runtime locking in onCreate() even though the activity should always be fixed. In that case, the manifest is simpler and clearer.
Finally, test on tablets, foldables, and devices with very different aspect ratios. An orientation policy that feels reasonable on one phone can feel wrong on larger screens.
Summary
- Use
android:screenOrientationin the manifest when an activity should always stay fixed. - Use
requestedOrientationat runtime when the orientation policy should change dynamically. - Do not confuse orientation locking with
configChangeshandling. - Lock orientation only when it is a genuine UX decision, not just a workaround for lifecycle bugs.
- Re-enable normal rotation with
SCREEN_ORIENTATION_UNSPECIFIEDwhen appropriate.
Related reading
- Prevent segue in prepareForSegue method?
- Prevent the keyboard from displaying on activity start
- Prevent the keyboard from displaying on activity start
- Print Entry, CFBundleIdentifier, Does Not Exist
- Print the size megabytes of Data in Swift
- print without newline in swift
- Printing a variable memory address in swift
- Priority Queue in swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.