简体   繁体   English

Android片段在FragmentTransaction之后还原TextView

[英]Android fragments restore TextView after FragmentTransaction

I have 2 fragments: FragmentA and FragmentB . 我有2个片段: FragmentAFragmentB Both fragments have basically the same code that looks like this: 两个片段的代码基本相同,如下所示:

public class FragmentB extends Fragment{

    private TextView textView;
    private EditText editText;
    private Button button;

    boolean visible = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState){
        final View rootView = inflater.inflate(R.layout.test_fragment, container, false);

        if (textView != null)
            Log.e("FragmentB", "Text was: " + textView.getText().toString());

        textView = (TextView) rootView.findViewById(R.id.textView);
        editText = (EditText) rootView.findViewById(R.id.editText);
        button = (Button) rootView.findViewById(R.id.button);
        ((TextView)rootView.findViewById(R.id.fragment)).setText("Fragment B");

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Log.e("FragmentB", "Set text: " + editText.getText());
                textView.setText(editText.getText());
                visible = !visible;
                textView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
            }
        });

        Log.e("FragmentB", "Text now: " + textView.getText());

        return rootView;
    }
}

To switch between the 2 fragments, i am calling fragmentTransaction.replace(R.id.main_content_frame, currentFragment); 要在两个片段之间切换,我正在调用fragmentTransaction.replace(R.id.main_content_frame, currentFragment); where currentFragment is the same instance that was created when the application started. 其中currentFragment是与应用程序启动时创建的实例相同的实例。 The problem is whenever i press the button in FragmentB to hide the TextView , switch to FragmentA , then back to FragmentB , the TextView is visible again, although i still want to be hidden. 问题是,每当我按下FragmentB中的按钮以隐藏TextView ,切换到FragmentA ,然后返回FragmentB ,即使我仍想隐藏,TextView再次可见。 Additionally, if i go back and forth one more time, the text in the TextView also gets reset, but only after going back and forth TWICE. 另外,如果我再来回一遍,TextView中的文本也会被重置,但只能在来回两次之后进行。

How can i keep the state of the fragments when switching between them? 在片段之间切换时如何保持片段的状态?

I have tried using onSaveInstanceState() and onViewStateRestored() , but onViewStateRestored() keeps getting called with the Bundle set to null . 我尝试使用onSaveInstanceState()onViewStateRestored() ,但是onViewStateRestored()总是在Bundle设置为null被调用。 Why does that happen? 为什么会这样呢?

Here is the code: 这是代码:

@Override
public void onViewStateRestored(Bundle savedInstanceState){
    Log.e("FragmentB", "Bundle: " + savedInstanceState);

    super.onViewStateRestored(savedInstanceState);
    try{
        textView.setText(savedInstanceState.getString("text"));
    }catch (NullPointerException e){
        e.printStackTrace();
    }

    Log.e("FragmentB", "Bundle: " + savedInstanceState);
    Log.e("FragmentB", "onViewStateRestored");
}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString("text", textView.getText().toString());
    Log.e("FragmentB", "onSaveInstanceState");
}

Using replace will destroy the fragment which is replaced . 使用replace将销毁被替换的片段。 So each time you switch fragment ,a new fragment will be created. 因此,每次切换片段时,都会创建一个新片段。 use hide and show will be better for you . 使用hideshow将更适合您。

fragmentTransaction.add(fragmentA).add(fragmentB).commit();
fragmentTransaction.show(fragmentA).hide(fragmentB).commit();

Try to use: 尝试使用:

fragmentTransaction.add(R.id.main_content_frame, currentFragment);

instead of: 代替:

fragmentTransaction.replace(R.id.main_content_frame, currentFragment);

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

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