user963241
user963241

Reputation: 7038

Textview not working in setRetainInstance

This is making me so fed up. No matter how I tried I cannot retain the state of textview upon the device is rotated.

The following is sample:

activity_main.xml

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

    <TextView
        android:layout_width="387dp"
        android:layout_height="103dp"
        android:text="New Text"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Text"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

RotationFragmentDemo.java

package com.rotationfragment;

import android.app.Activity;
import android.os.Bundle;

public class RotationFragmentDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This check returns 'null' for the first time when there are no fragments
        if (getFragmentManager().findFragmentById(android.R.id.content) == null) {

            getFragmentManager().beginTransaction().add(android.R.id.content, new RotationFragment()).commit();
        }
    }
}

RotationFragment.java

  package com.rotationfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RotationFragment extends Fragment implements
        View.OnClickListener {

    TextView textView;

    String myText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState) {

        // This should help to retain fragment
        setRetainInstance(true);

        View result=inflater.inflate(R.layout.activity_main, parent, false);

        result.findViewById(R.id.button).setOnClickListener(this);

        if (textView == null)
            textView = (TextView)result.findViewById(R.id.textView);
        else
            textView.setText(myText);

        return(result);
    }


    @Override
    public void onClick(View v) {

        textView.setText("Hello.");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        myText = textView.getText().toString();
    }
}

In the debug mode when the device is rotated, I clearly see the Text is set to textview in onCreateView at line textView.setText(myText); but still this is not working as expected.

Upvotes: 2

Views: 167

Answers (2)

B&#246; macht Blau
B&#246; macht Blau

Reputation: 13009

The problem with your code is that the Fragment is retained (myText keeps its value after an orientation change) but there will be a new set of Views, and so you have to bind to them in onCreateView() or your code will keep referring to the now invisible Views

Just for testing, I used the following method

@Override
public void onViewStateRestored(Bundle savedInstanceState)
{
    boolean same = (textView.equals(getView().findViewById(R.id.textView)));
    Log.d(TAG, "onViewStateRestored: TextViews are same object:" + same);
    super.onViewStateRestored(savedInstanceState);
}

and got the following Logcat entry after rotating the device:

com.example.retainedrotationapplication D/RotationFragment: onViewStateRestored: TextViews are same object:false

So you have skip the null check in onCreateView() and write

textView = (TextView)result.findViewById(R.id.textView);

Upvotes: 1

JavadBadirkhanly
JavadBadirkhanly

Reputation: 645

You retain fragment, but when you rotate screen, activity recalls onCreate method and this redraws your RotationFragment. You can force for saving with this manifest line.

android:configChanges="keyboardHidden|orientation|screenSize"

Upvotes: 0

Related Questions