Vitaly Zinchenko
Vitaly Zinchenko

Reputation: 4911

How to restore the state of a detached view

Here's a simple project:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/add_some_layout_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add layout"/>

</LinearLayout>

some_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittext_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/some_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Some hint"
        android:inputType="text"/>

</LinearLayout>

Let's consider two cases:


Case 1:

public class MainActivity extends AppCompatActivity {

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

        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        final LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        final View someLayoutWithEditText = layoutInflater.inflate(R.layout.some_layout, container, false);
        container.addView(someLayoutWithEditText);

        // button isn't used
    }
}

Effect of case 1: EditText retains its text during configuration changes.


Case 2:

public class MainActivity extends AppCompatActivity {

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

        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        final LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
        final View someLayoutWithEditText = layoutInflater.inflate(R.layout.some_layout, container, false);

        Button addLayoutButton = (Button) findViewById(R.id.add_some_layout);
        addLayoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                container.addView(someLayoutWithEditText);
            }
        });
    }
}

Effect of case 2: We add the EditText on button click => after all "restore part" has been done => the state of the EditText wasn't restored


Question: How to restore the state of the EditText in the second case?

Upvotes: 0

Views: 108

Answers (1)

Orest Savchak
Orest Savchak

Reputation: 4569

Your state restoring is done in onRestoreInstanceState() method, it is called by system after onCreate, so if you add your view after restoring, you state will be lost.

So if you want to save state of your view and restore it manually, not by system, you can do next:

1)Override onSaveInstanceState() method and save value inside EditText in Bundle parameter. 2)Get it from bundle and set it manually into the EditText.

Upvotes: 1

Related Questions