I'm trying to understand the Instance state concept in Android. The documentation available at https://developer.android.com/guide/components/activities/activity-lifecycle describes that
The saved data that the system uses to restore the previous state is called the instance state and is a collection of key-value pairs stored in a Bundle object. By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText widget). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.
Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.
I made an example with all the requisites described above, nevertheless it still doesn't work as I expected: after clicking the button, and changing the background color of LinearLayout, which have an unique Id, it looses color after rotating screen. The radio button remains selected as expected though.
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>
and MainActivity.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);
}
}
Am I doing something wrong, misunderstood the functionality or is there any reason that make it not saving also the backgroundColor of LinearLayout?
Thanks
P.S.: I already get it working, coding, I just want to know which properties are covered automatically!