Textview无法在setRetainInstance中工作

时间:2017-06-16 12:27:47

标签: android android-fragments textview orientation

这让我非常厌倦。无论我如何尝试,我都无法在旋转设备时保留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();
    }
}

在旋转设备的调试模式下,我清楚地看到文本在onCreateView的第textView.setText(myText);行设置为textview,但仍然没有按预期工作。

2 个答案:

答案 0 :(得分:1)

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

仅用于测试,我使用以下方法

@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是同一个对象:false

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

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

答案 1 :(得分:0)

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

android:configChanges="keyboardHidden|orientation|screenSize"