Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't TextView (with an ID) automatically save it's state?

My understanding was that all standard Views with an ID should save their state automatically, and upon trying this on an example I found it to be quite confusing.

I had only 1 activity and the main layout as listed below.
When I change the text of the TextView by clicking on the button, and then rotate the screen, the TextView does actually save it's state, but upon rotating again, it resets to it's default state.

Same happens if I edit it while in landscape and rotate: after first rotate it still saves it's state, but after another rotate (unless I change the text again) it resets to default value.

I'm really confused now. Could someone please explain this behavior to me. I wasn't able to find any other question (or answer) that explains this specific behavior.

activity_main.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/activity_main_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:text="test"
        android:textSize="50sp"
        />

    <EditText
        android:id="@+id/activity_main_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:maxLength="30"
        android:maxLines="1"
        />

    <Button
        android:id="@+id/activity_main_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Set text"
        />

</RelativeLayout>

MainActivity.java:

package com.example.viewstatetest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText editText;
    private View button;

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

        textView = (TextView) findViewById(R.id.activity_main_textView);
        editText = (EditText) findViewById(R.id.activity_main_editText);
        button = findViewById(R.id.activity_main_button);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(editText.getText());
            }
        });
    }
}
like image 347
SuperLemon Avatar asked Jul 21 '15 00:07

SuperLemon


1 Answers

If you look at the TextView sources (in particular TextView#onSaveInstanceState()), you will see that text is saved only in two cases:

  1. When user explicitly asked to do that by setting freezeText flag to true via android:freezesText="true" or TextView#setFreezesText(true)
  2. When text within TextView has selection.

In your case both of these cases are false, so your text gets reset to its default state during orientation change.

Simple change to your layout will solve the issue:

<TextView
    android:id="@+id/activity_main_textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:freezesText="true"
    android:maxLines="2"
    android:text="test"
    android:textSize="50sp"
    />
like image 176
Pavel Dudka Avatar answered Oct 11 '22 19:10

Pavel Dudka