Home » Android » android – Restoring state of TextView after screen rotation?

android – Restoring state of TextView after screen rotation?

Posted by: admin March 11, 2020 Leave a comment


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?

How to&Answers:

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

     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


In order to retain data on orientation change you need to implement the two methods:

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // Read values from the "savedInstanceState"-object and put them in your textview

protected void onSaveInstanceState(Bundle outState) {
    // Save the values you need from your textview into "outState"-object


1) Not all views with an ID save their state. Android widgets, with an ID, whose state can be changed by the user, appear to save their state on a soft kill. So EditText saves its state, but TextView does not save its state on a soft kill.

“AFAIK, Android only bothers saving state for things that are expected to
change. That is why it saves the text in an EditText (which a user is
likely to change) and perhaps does not save the state for a TextView (which normally stays static)”

Mark M

So you may choose to save the state of the textview in onSaveInstanceState and you may choose to restore the state of the textview in onCreate.

2) Best practice is to save “internal” non view instance state even if you declare

android:configChanges= "orientation|keyboardHidden" 

From the docs:

“However, your application should always be able to shutdown and restart with its previous state intact. Not only because there are other configuration changes that you cannot prevent from restarting your application but also in order to handle events such as when the user receives an incoming phone call and then returns to your application.”



Android does not handle that kind of stuff for you. You have save all data manually.

You can use in a activity to save values

    public Object onRetainNonConfigurationInstance() {
        HashMap<String, Object> savedValues = new HashMap<String, Object>();
        savedValues.put("someKey", someData);           
        return savedValues;

and use something like this in the oncreate method of a activity to load the saved objects

HashMap < String, Object> savedValues 
     = (HashMap<String, Object>)this.getLastNonConfigurationInstance();

you can also choose the disable orientation change for an activity

<activity android:name=".Activity" android:screenOrientation="portrait" >
        <action android:name="android.intent.action.MAIN" />


Modify your activities in AndroidManifest.xml to override orientation change behavior by adding this to your activity/activities:


It should look somewhat like this:

    <activity android:name=".activity.LoginActivity"


Add these to AndroidManifest.xml

Put your Activity Name in the place of Activity_Name

<activity android:name="Activity_Name"

This will work with value changing TextField also.


just add


to your Activity in AndroidManifest.xml.


When configuration changes Android restarts your activity by default. To change this you should override onConfigurationChanged() method. Also, you should add android:configChanges to your manifest file.

You can read more here.


I also used


and it did not work.

But then I found a solution.

Verify if you have the following line correctly in your maniest:

<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="11" />

make sure it does not read something like

<uses-sdk android`:minSdkVersion="11" android:targetSdkVersion="15" />`

I had it the second way in first place but when I corrected it state got preserved.