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?
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().
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.
you should use this: myText. setVisibility(View. INVISIBLE);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With