简体   繁体   English

如何在片段中保存此 TextView 的实例状态?

[英]How can I save the instance state of this TextView in a fragment?

I have the text inputted by the user in an EditText replace the text of a TextView upon a button press.我让用户在 EditText 中输入的文本在按下按钮时替换 TextView 的文本。 Once the TextView text has been changed, when I navigate to another fragment or activity and come back, the TextView has reverted back to its original text.更改 TextView 文本后,当我导航到另一个片段或活动并返回时,TextView 已恢复为其原始文本。 I have tried multiple solutions to save the instance state to no avail.我尝试了多种解决方案来保存实例状态无济于事。

Edit2: The is the latest code from the fragment Edit2:这是片段中的最新代码

public class TeamFragment extends Fragment {

private TextView testTextView;
private EditText testEditText;
private SharedPreferences pref;
private SharedPreferences.Editor editor;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    View v = inflater.inflate(R.layout.fragment_team, container, false);


    Button button = (Button)v.findViewById(R.id.recordBtn);
    testEditText = v.findViewById(R.id.testEditText);
    pref = this.getActivity().getSharedPreferences("my_pref",
            Context.MODE_PRIVATE);
    editor = pref.edit();

    testTextView = (TextView) v.findViewById(R.id.testTextView);
    final EditText testEditText = (EditText) v.findViewById(R.id.testEditText);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            testTextView.setText(testEditText.getText().toString());
            editor.putString("val", testEditText.getText().toString());
            editor.apply();

        }
    });
    return v;

}

@Override
public void onStart() {
    super.onStart();
    String val = this.getActivity().getSharedPreferences("my_pref",
            Context.MODE_PRIVATE).getString("val", "n/a");
    if(val.equals("n/a")) {
        testTextView.setText(val);
    }

}

}

This is my layout file这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:padding="8dp">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Record"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/tresEditText"
    android:id="@+id/recordBtn"
    android:onClick="displayEditText"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/testTextView"
    android:text="Test"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="30dp"/>
<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="100dp"
    android:id="@+id/testEditText"/>

</RelativeLayout>

Your app crashes because you have not initialized EditText and TextView .您的应用程序崩溃是因为您尚未初始化EditTextTextView

Instead of overriding onSaveInstanceState() , consider using SharedPreferences() as onSaveInstanceState() is used to save the state when the device orientation is changed.与其覆盖onSaveInstanceState() ,不如考虑使用SharedPreferences()因为onSaveInstanceState()用于在设备方向更改时保存状态。 In your case, what you can do is to save textView data in SharedPreference and when you come back to the fragment, you can check in 'onStart()' whether the SharedPreferences() contain the saved value, then populate your textView accordingly.在您的情况下,您可以做的是将 textView 数据保存在SharedPreference ,当您返回片段时,您可以检查“onStart()”是否SharedPreferences()包含保存的值,然后相应地填充您的 textView。

You can do something like following:您可以执行以下操作:

public class TeamFragment extends Fragment {

private TextView testTextView;
private EditText testEditText;
private SharedPreferences pref;
private SharedPreferences.Editor editor;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

View v = inflater.inflate(R.layout.fragment_team, container, false);

Button button = (Button)v.findViewById(R.id.recordBtn);
textTextView = v.findViewById(R.id.your_id);
testEditText = v.findViewById(R.id.your_id);
pref = this.getActivity().getSharedPreferences("my_pref", 
Context.MODE_PRIVATE);
editor = pref.edit();

testTextView = (TextView) v.findViewById(R.id.testTextView);
final EditText testEditText = (EditText) 
v.findViewById(R.id.testEditText);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        testTextView.setText(testEditText.getText().toString());
        editor.putString("val", testEditText.getText().toString());
        editor.apply();

    }
});
return v;

}

@Override
protected void onStart() {
    super.onStart();
    String val = this.getActivity().getSharedPreferences("my_pref", 
    Context.MODE_PRIVATE).getString("val", "n/a");
    if(val.equals("n/a")) {
      testTextView.setText(val);
    }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM