Sự hiểu biết của tôi là tất cả các Chế độ xem chuẩn có ID sẽ tự động lưu trạng thái của chúng và khi thử điều này trên ví dụ, tôi thấy nó khá khó hiểu.Tại sao TextView (có ID) tự động lưu trạng thái của nó?
Tôi chỉ có 1 hoạt động và bố cục chính như được liệt kê bên dưới.
Khi tôi thay đổi văn bản của TextView bằng cách nhấp vào nút, và sau đó xoay màn hình, TextView thực sự lưu trạng thái của nó, nhưng khi quay lại, nó sẽ đặt lại về trạng thái mặc định của nó.
Tương tự xảy ra nếu tôi chỉnh sửa trong khi ở chế độ ngang và xoay: sau khi xoay đầu tiên nó vẫn lưu trạng thái của nó, nhưng sau khi xoay khác (trừ khi tôi thay đổi lại văn bản), giá trị này sẽ đặt lại về giá trị mặc định.
Tôi thực sự bối rối ngay bây giờ. Ai đó có thể vui lòng giải thích hành vi này cho tôi. Tôi không thể tìm thấy bất kỳ câu hỏi nào khác (hoặc câu trả lời) giải thích hành vi cụ thể này.
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());
}
});
}
}
Hãy thử điều này có lẽ: http://stackoverflow.com/questions/5123407/losing-data-when-rotate-screen – Zarwan
Cảm ơn, nhưng mà chương trình làm thế nào để lưu trạng thái. Tôi biết cách lưu trạng thái theo cách thủ công, nhưng muốn biết tại sao một số Chế độ xem (đôi khi!) Sẽ không tự động lưu trạng thái của chúng. – SuperLemon