Textview 在 setRetainInstance 中不起作用

Textview not working in setRetainInstance

这让我受够了。无论我如何尝试,我都无法在设备旋转时保留 textview 的状态。

示例如下:

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();
    }
}

在设备旋转时的调试模式下,我清楚地看到 onCreateViewtextView.setText(myText); 中的文本设置为文本视图,但这仍然没有按预期工作。

您保留片段,但是当您旋转屏幕时,activity 调用 onCreate 方法,这会重绘您的 RotationFragment。您可以使用此清单行强制保存。

android:configChanges="keyboardHidden|orientation|screenSize"

你的代码的问题是 Fragment 被保留(myText 在方向改变后保持它的值)但是会有一组新的 Views,所以你必须在 onCreateView() 中绑定它们,否则你的代码将继续引用现在不可见的 Views

为了测试,我使用了下面的方法

@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);
}

并在旋转设备后得到以下 Logcat 条目:

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

所以你跳过了 onCreateView() 中的空检查并写入

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