15

I did a simple application with 2 buttons (start and exit) and an empty TextView. If i push the start button, a TextView will be update. My problem is that if i rotate the device, my layout will restart (if start was pushed, the TextView will become empty again. Why?

As As
  • 2,049
  • 4
  • 17
  • 33

3 Answers3

7

It because activity recreate after screen rotate. Simple fix is adding android:id to your TextView at xml.

vmtrue
  • 1,724
  • 18
  • 36
1

Android automatically destroys the activity and recreates it adapting it to the new orientation of the screen when the device is rotated.

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28
  • 2
    Avoidance of the activity destruction and recreation is inevitable but what you can do is force the activity to maintain a fixed orientation. or save what you need to in the saveInstanceState(Bundle) method of the activity and later retireve it from the same bundle passed on to it via the onCreate(Bundle); or restoreInstanceState(); also ingoring onConfigChange() is an option but is highly discouraged. – Niraj Adhikari Nov 06 '13 at 15:12
  • 3
    I noticed only one post didn't get downvoted and that was fishers. Fishy. – Samuel Nov 06 '13 at 15:25
  • 2
    @Samuel Could be because his is the only correct answer whilst the ones which have been downvoted are simply wrong? – Simon Nov 06 '13 at 19:14
-3

If you dont want activity to recreate on orientation change you can add this line

android:configChanges="orientation|keyboardHidden"

in Android manifet for that Activity.. for eg:-

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

Reference here

CRUSADER
  • 5,486
  • 3
  • 28
  • 64