哪些属性会自动保存,无需额外编码?屏幕旋转后未在线性布局中恢复背景颜色



我试图理解Android中的实例状态概念。https://developer.android.com/guide/components/activities/activity-lifecycle 提供的文档描述了

系统用于恢复先前状态的已保存数据称为实例状态,是存储在 Bundle 对象中的键值对的集合。默认情况下,系统使用 Bundle 实例状态来保存有关活动布局中每个 View 对象的信息(例如在 EditText 构件中输入的文本值(。因此,如果活动实例被销毁并重新创建,则布局的状态将还原到其以前的状态,而无需任何代码。但是,您的活动可能包含更多要还原的状态信息,例如跟踪用户在活动中进度的成员变量。

注意:为了让 Android 系统恢复您活动中数据视图的状态,每个数据视图都必须具有唯一的ID,该 ID 由 android:id 属性提供。

我用上述所有必要条件做了一个例子,但它仍然没有按我的预期工作:单击按钮并更改具有唯一 ID 的 LinearLayout 的背景颜色后,它在旋转屏幕后会失去颜色。不过,单选按钮仍按预期保持选中状态。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.widget.LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerHorizontal"
android:orientation="vertical"
android:padding="16dp"
android:showDividers="middle">

<android.widget.LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Question 1?" />

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_p1o1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option1" />
<RadioButton
android:id="@+id/radio_p1o2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option2" />
<RadioButton
android:id="@+id/radio_p1o3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="option3" />
</RadioGroup>
</android.widget.LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:onClick="changeColor"
android:text="Change Background Color" />
</android.widget.LinearLayout>
</ScrollView>

和主活动.java

package com.example.android.testlinearlayoutpropertiessave;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import static android.graphics.Color.GREEN;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void changeColor(View view) {
findViewById(R.id.layout1).setBackgroundColor(GREEN);
}
}

我是否做错了什么,误解了功能,或者有任何原因使其不保存线性布局的背景颜色?

谢谢

PS:我已经让它工作了,编码,我只想知道哪些属性是自动覆盖的!

必须定义一个标志来指示状态更改,更新 changeColor 方法中标志的值,并在 SaveInstanceState 上实现该方法。例:

package com.example.android.testlinearlayoutpropertiessave;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import static android.graphics.Color.GREEN;
public class MainActivity extends AppCompatActivity {
private boolean colorChanged = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
colorChanged = savedInstance.getBoolean("colorChanged");
}
if (colorChanged) {
findViewById(R.id.layout1).setBackgroundColor(GREEN);
}
}
public void changeColor(View view) {
findViewById(R.id.layout1).setBackgroundColor(GREEN);
colorChanged = true;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(colorChanged);
}
}

您必须检索savedInstanceState才能对其进行任何操作。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore from saved state
} else {
// New instance
}
setContentView(R.layout.activity_main);
}

这应该回答任何剩余的问题

这完全取决于您在savedInstanceState捆绑包中保存的内容。

您只需在onSavedInstanceState调用中使用savedInstanceState.putInt("savedNum", 300);,然后在返回视图后 - 在onCreate中,检查savedInstanceState是否不为 null 并使用int oldNum = savedInstanceState.getInt("savedNum");检索值

最新更新