Screen Orientation (Portrait, Landscape)

You control whether a screen shows upright or sideways with the android:screenOrientation attribute on the <activity> in the manifest (portrait, landscape, sensor, and more), and by default rotating the device destroys and recreates the Activity, so unsaved state is lost unless you handle it.

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The form that emptied itself

A student half-fills the FestConnect form, then turns the phone sideways to type more comfortably. Every field they typed is suddenly blank.

That is not a bug you wrote; it is Android's default behaviour. Rotating the screen is a configuration change, and by default Android destroys and recreates the Activity, wiping unsaved state. You control this with one manifest attribute, android:screenOrientation, and by understanding what rotation actually does.

Theory

Rebuilding the room when you turn the box

Imagine a dollhouse room where, every time you rotate the whole house, the furniture is cleared out and the room is set up fresh from the blueprint. Anything you had rearranged by hand is gone. Android does this on rotation: it tears down the Activity and rebuilds it from onCreate. Either you save your arrangement first, or you stop the house from rotating.

Theory

Setting and understanding orientation

Set it in the manifest on the Activity:

  • portrait: locked upright.
  • landscape: locked sideways.
  • sensor / fullSensor: follow the device's rotation sensor.
  • unspecified: the system decides (the default).

The catch: a rotation is a configuration change, and by default it destroys and recreates the Activity (onCreate runs again), losing unsaved UI state. Locking the orientation prevents the rotate, and thus the reset; alternatively, save state in onSaveInstanceState.

Practical

Lock the form to portrait (manifest)

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- screenOrientation="portrait" -> the screen will not rotate,
     so the Activity is not recreated, so the half-filled
     FestConnect form keeps its values. -->

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Why did rotating clear the fields?

With the default orientation, turning the phone wiped the FestConnect form. Explain the chain of events, and give two different fixes.

Show the answer

Rotating triggers a configuration change; by default Android destroys the Activity and recreates it, running onCreate again from scratch, so the in-memory field values are gone. Two fixes: (1) lock the orientation with android:screenOrientation="portrait", so no rotation, no recreate; or (2) save and restore the values in onSaveInstanceState/onCreate so they survive the rebuild. Locking is simplest for a form; saving state is the more general, robust habit.

Quiz

By default, what happens to an Activity when the user rotates the device?

  1. It is destroyed and recreated (onCreate runs again), losing unsaved UI state
  2. Nothing happens; the Activity keeps running untouched
  3. The app closes completely
  4. Only the manifest is reloaded, not the Activity
Show the answer

It is destroyed and recreated (onCreate runs again), losing unsaved UI state

A rotation is a configuration change, and by default Android destroys and recreates the Activity, calling onCreate again, which is why unsaved fields reset (A). The Activity does not simply keep running untouched (B), the app does not close (C), and the manifest is not 'reloaded' at runtime (D). This recreate-on-rotate behaviour is the reason to either lock orientation or save state.

Watch out

Orientation traps

1. Assuming rotation preserves state: by default it does not; the Activity is recreated. Lock orientation or save state.

2. Confusing orientation with responsive layout: screenOrientation locks which way up; making a layout adapt to landscape is a separate design task (alternate layout resources).

3. Hard-locking everything to portrait as a crutch: fine for a form, but it hides the real lesson, that Activities must survive recreation.

Theory

Next: put controls on the screen

Your FestConnect screen is clean (no title bar) and stable (locked orientation). Now it needs actual inputs. The next lesson places the core form widgets, an EditText for the name and a Button, and wires the Button's onClick to read the field, the first real interactivity in your Android app.

Summary

Key takeaways

  • Set android:screenOrientation on the <activity> in the manifest: portrait, landscape, sensor, unspecified (default).
  • Rotation is a configuration change; by default Android destroys and recreates the Activity (onCreate runs again).
  • That recreate wipes unsaved UI state, which is why a half-filled form clears on rotate.
  • Fix by locking orientation (portrait) or saving/restoring state in onSaveInstanceState.
  • screenOrientation (which way up) is different from designing a responsive landscape layout.
  • Memory hook: turning the box rebuilds the room from the blueprint.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Screen Orientation (Portrait, Landscape) · Mobile Application Development - 1 (option B) · Gri-Learn