Theory
Form जो ख़ुद को खाली कर गया
एक student FestConnect form आधा भरता है, फिर ज़्यादा आराम से type करने के लिए phone को sideways घुमाता है। उन्होंने जो भी typed किया था वह सारे fields अचानक खाली हैं।
यह कोई bug नहीं है जो आपने लिखा; यह Android का default behaviour है। Screen rotate करना एक configuration change है, और default रूप से Android Activity को destroy और recreate करता है, unsaved state मिटाते हुए। आप इसे एक manifest attribute से control करते हैं, android:screenOrientation, और यह समझकर rotation actually क्या करता है।
Theory
Box घुमाने पर room rebuild होना
एक dollhouse room सोचिए जहाँ, हर बार जब आप पूरा house rotate करते हैं, furniture साफ़ कर दिया जाता है और room blueprint से फिर से set up होता है। आपने हाथ से जो भी rearrange किया वह चला जाता है। Android rotation पर यही करता है: यह Activity को तोड़ता है और इसे onCreate से rebuild करता है। या तो आप पहले अपना arrangement save कर लें, या house को rotate होने से रोकें।
Theory
Orientation set करना और समझना
Activity पर manifest में इसे set कीजिए:
- portrait: upright locked।
- landscape: sideways locked।
- sensor / fullSensor: device के rotation sensor को follow करता है।
- unspecified: system decide करता है (default)।
Catch: एक rotation एक configuration change है, और default रूप से यह Activity को destroy और recreate करता है (onCreate फिर चलता है), unsaved UI state खोते हुए। Orientation lock करना rotate को रोकता है, और इसलिए reset को; alternatively, onSaveInstanceState में state save कीजिए।
Practical
Form को portrait पर lock कीजिए (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
Rotate करने पर fields क्यों साफ़ हो गए?
Default orientation के साथ, phone घुमाना FestConnect form मिटा गया। Events की chain explain कीजिए, और दो अलग fixes दीजिए।
Show the answer
Rotate करना एक configuration change trigger करता है; default रूप से Android Activity को destroy करता है और recreate करता है, scratch से फिर onCreate चलाते हुए, तो memory में fields की values चली जाती हैं। दो fixes: (1) `android:screenOrientation="portrait"` से orientation lock कीजिए, तो कोई rotation नहीं, कोई recreate नहीं; या (2) onSaveInstanceState/onCreate में values को save और restore कीजिए ताकि वे rebuild से बच जाएँ। एक form के लिए lock करना सबसे simple है; state save करना ज़्यादा general, robust habit है।
Quiz
Default रूप से, जब user device rotate करता है तो एक Activity का क्या होता है?
- यह destroy और recreate होती है (onCreate फिर चलता है), unsaved UI state खोते हुए
- कुछ नहीं होता; Activity untouched चलती रहती है
- App पूरी तरह बंद हो जाती है
- सिर्फ़ manifest reload होता है, Activity नहीं
Show the answer
यह destroy और recreate होती है (onCreate फिर चलता है), unsaved UI state खोते हुए
एक rotation एक configuration change है, और default रूप से Android Activity को destroy और recreate करता है, onCreate फिर call करते हुए, यही वजह है unsaved fields reset हो जाते हैं (A)। Activity बस untouched नहीं चलती रहती (B), app बंद नहीं होती (C), और manifest runtime पर 'reload' नहीं होता (D)। यह recreate-on-rotate behaviour ही वजह है orientation lock करने या state save करने की।
Watch out
Orientation traps
1. मान लेना rotation state preserve करता है: default रूप से नहीं करता; Activity recreate होती है। Orientation lock कीजिए या state save कीजिए।
2. Orientation को responsive layout से confuse करना: screenOrientation तय करता है कौन सी तरफ़ ऊपर है; एक layout को landscape में adapt कराना एक अलग design task है (alternate layout resources)।
3. सब कुछ hard-lock portrait पर करना एक crutch की तरह: एक form के लिए ठीक है, पर यह असली lesson छुपा देता है, कि Activities को recreation से बचना ज़रूरी है।
Theory
अगला: screen पर controls रखना
आपकी FestConnect screen साफ़ है (कोई title bar नहीं) और stable है (locked orientation)। अब इसे असली inputs चाहिए। अगला lesson core form widgets रखता है, नाम के लिए एक EditText और एक Button, और Button के onClick को field पढ़ने के लिए wire करता है, आपकी Android app में पहली असली interactivity।
Summary
Key takeaways
- Manifest में <activity> पर android:screenOrientation set कीजिए: portrait, landscape, sensor, unspecified (default)।
- Rotation एक configuration change है; default रूप से Android Activity को destroy और recreate करता है (onCreate फिर चलता है)।
- वह recreate unsaved UI state मिटा देता है, यही वजह है rotate पर एक आधा भरा form clear हो जाता है।
- Orientation lock करके (portrait) या onSaveInstanceState में state save/restore करके fix कीजिए।
- screenOrientation (कौन सी तरफ़ ऊपर) एक responsive landscape layout design करने से अलग है।
- Memory hook: box घुमाना blueprint से room rebuild करता है।