10

I am writing a phone dialer app for android. I created a layout for keypad, which contains a TextView and 10 buttons. Buttons are as keys for 10 digits(0 to 9) and TextView is for displaying the number according to keys pressed.

In my app, i am appending the text ("0" or "1", etc.) to the TextView for each button pressed. If i pressed the buttons 1, 2, 3 then the text on TextView is 123. The problem is, let's take the screen is in landscape mode and TextView contains 123, if i turn it, in portrait mode no text on TextView.

Please Help Me Regarding this.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
  • Check this one http://stackoverflow.com/questions/3542333/how-to-prevent-custom-views-from-losing-state-across-screen-orientation-changes/3542895#3542895 – om252345 Jan 16 '12 at 11:43
  • 1
    Does your TextView contain id? Do you use fragments? – ania Jan 16 '12 at 11:44
  • id is there but i am not using fragments. – Yugandhar Babu Jan 16 '12 at 11:48
  • 1
    add this in your java class `@Override public void onConfigurationChanged(final Configuration newConfig) { // Ignore orientation change to keep activity from restarting super.onConfigurationChanged(newConfig); }` and in your manifest file add this `` this should probably work. – Mukunda Jan 16 '12 at 11:51
  • @Mukunda thankyou its working now. But only **android:configChanges="orientation** is sufficient. – Yugandhar Babu Jan 16 '12 at 11:58
  • @YugandharBabu yes only **orientation** is enough but android documentation says **orientation|keyboardHidden** and later versions after 2.3 you also need to add **screenSize** – Mukunda Jan 16 '12 at 12:02

5 Answers5

16

What @jeet recommended didn't work for me. I had to add "screenSize". This is the line you should add in your manifest.xml in the <activity> node of your activity:

android:configChanges="keyboardHidden|orientation|screenSize"

Thus, the complete node may look like this:

<activity
android:name=".YourActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar">
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
user160446
  • 311
  • 2
  • 6
9

Please check on orientation change, on create method is called, which requires all the views to be created again, so you need to use one of the following methods:

  1. use onSavedInstance method and save the states of components/views to bundle.
  2. Just use following flag true in your manifest file in activity tag android:configChanges="keyboardHidden|orientation". like below:

    <activity android:name=".SampleActivity" android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation">
        ...
    </activity>
    
Jaroslav Záruba
  • 4,694
  • 5
  • 39
  • 58
jeet
  • 29,001
  • 6
  • 52
  • 53
8

The reason for this is due to Android basically destroying the activity and creating it again every time you rotate the device. This is mainly to allow for different layouts based on portrait/landscape mode.

The best way to handle this is to store whatever data you need to keep within the Activity Bundle, by responding to the onSavedInstance event (called just before Android destroys the activity), and then reapplying those in the standard onCreate event.

Although you can add "orientation" to the configChanges property, keep in mind that you're basically telling Android that you're going to be handling everything relating to orientation change yourself - including changing layout, etc.

Digital_Utopia
  • 816
  • 8
  • 17
2

To preserve a TextView's text, you can simply set the TextView's freezesText property to true. As in:

    <TextView
    ...
    android:freezesText="true"
    .../>

This is the accepted answer here: Restoring state of TextView after screen rotation?

Sam
  • 1,009
  • 10
  • 13
0

If someone is still having troubles... this did the trick for me

public class BranjeKP extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_branje_kp);

    //...
}

@Override
protected void onSaveInstanceState(Bundle out) {
    super.onSaveInstanceState(out);
    out.putString("TVNazivPod", TVNazivPodatka.getText().toString());
    out.putString("TVEnotaMere", TVEnotaMere.getText().toString());
}

@Override
protected void onRestoreInstanceState(Bundle in) {
    super.onRestoreInstanceState(in);
    TVNazivPodatka.setText(in.getString("TVNazivPod"));
    TVEnotaMere.setText(in.getString("TVEnotaMere"));
}

You basically save any values you want before rotation (that's when onSaveInstanceState is called) into a Bundle and after rotation (onRestoreInstanceState) you just pull all values out from Bundle. Just to clarify, TVNazivPodatka and TVEnotaMere are TextView widgets.

...with a help from How to prevent custom views from losing state across screen orientation changes