Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setRetainInstance not retaining the instance

I'm trying to use setRetainInstance() but it seems not working for me! =(. I simplified the problem to the easiest version:

I have a fragment with a TextView and a Button.

The initial text of the TextView is "I'm waiting" when the button is pressed the text changes to "Hello!"

That is my code:

The fragment:

public class SayHelloFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.say_hello_layout, container);
        final TextView text = (TextView) view.findViewById(R.id.textView1);
        view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                text.setText("Hello!");

            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

}

The Activity:

public class MainActivity extends FragmentActivity {

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

}

The activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/say_hello_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.example.retaintext.SayHelloFragment" />

The Fragment's layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I&apos;m Waiting"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

But when I play it, press the button and change the device's orientation the text becomes reset.

What am I missing? How can I solve it? Thanks!

like image 537
Addev Avatar asked Aug 19 '12 12:08

Addev


1 Answers

You are missing that onCreateView runs after the rotation again and inflates the default view with the default text again.

You need to restore the state of your views manually, e.g.:

public class SayHelloFragment extends Fragment {

    private String mText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.say_hello_layout, container);
        final TextView text = (TextView) view.findViewById(R.id.textView1);
        if (mText != null)
            text.setText(mText);

        view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mText = "Hello!";
                text.setText(mText);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

}
like image 91
zapl Avatar answered Sep 24 '22 01:09

zapl