调用了 Fragment 的 onDestroyView 但视图未被销毁

Fragment's onDestroyView called but view is not being destroyed

Android片段生命周期表明,当一个片段被添加到后台堆栈然后removed/replaced,onDestroyView()被调用,稍后,当片段returns 从后台堆栈到布局,onCreateView() 被调用。

根据我的理解,这意味着片段的视图正在被销毁并重新创建。如果用户在片段 A 的 EditText 中输入文本,然后转到片段 B,然后返回到片段 A,当片段返回时,EditText 的内容将被删除。

然而,这并没有发生在下面的代码中;谁能解释为什么?我已经确认正在调用 FragmentAonDestroyView()

MainActivity.java

public class MainActivity extends FragmentActivity {

    private Fragment currentFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            addFragment(new FragmentA());
        }
    }

    public void setCurrentFragment(Fragment fragment) {
        this.currentFragment = fragment;
    }

    @Override
    public void onBackPressed() {
        if (currentFragment instanceof FragmentB) {
            getSupportFragmentManager().popBackStackImmediate();
        } else {
            super.onBackPressed();
        }
    }

    public void addFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).addToBackStack(null).commit();
        setCurrentFragment(fragment);
    }
}

FragmentA.java

public class FragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        final EditText editText = (EditText)view.findViewById(R.id.editText);

        Button button = (Button)view.findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentB fragmentB = new FragmentB();
                Bundle arguments = new Bundle();
                arguments.putString("text", editText.getText().toString());
                fragmentB.setArguments(arguments);
                ((MainActivity)getActivity()).addFragment(fragmentB);
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("Tag", "FragmentA.onDestroyView() has been called.");
    }
}

FragmentB.java

public class FragmentB extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        TextView textView = (TextView)view.findViewById(R.id.textView);
        textView.setText(getArguments().getString("text"));
        return view;
    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fragmenttest.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_a.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

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

</LinearLayout>

fragment_b.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

返回堆栈中的片段在 Bundle 中保留其状态。这包括具有 EditText 等当前内容的视图层次结构状态。

摧毁碎片..使用这个onDestroyViewonDetach

     if (fragment != null) {

             fragment =  new YourFragment();
               FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
               ft.remove(fragment);
               ft.commit();
        }