Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring state of TextView after screen rotation?

In my app I have TextView and EditText. Both have data in it. When the screen orientation changes the data in the EditText remains, but TextView data is cleared.

Can some one help me out to find a way to retain data in TextView too?

like image 894
kAnNaN Avatar asked Mar 03 '11 10:03

kAnNaN


People also ask

When screen recreates from rotation from which method we can restore the data?

You can save Primitive data such as String, Boolean, Integers or Parcelable objects in a Bundle during the orientation change and read the same data when Activity recreated. Saving and restoring the data works using two Activity lifecycle methods called onSaveInstanceState() and onRestoreInstanceState().

Why an activity is destroyed and recreated when you rotate your screen?

Background. When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do I make Textview disappear?

you should use this: myText. setVisibility(View. INVISIBLE);


1 Answers

If you want to force your TextView to save its state you must add freezesText attribute:

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

From documentation on freezesText :

If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider

like image 141
inazaruk Avatar answered Sep 19 '22 16:09

inazaruk