片段的onDestroyView被调用,但视图没有被销毁

时间:2015-02-05 13:01:23

标签: android android-fragments

Android片段生命周期显示,当片段被添加到backstack然后被删除/替换时,onDestroyView()被调用,稍后,当片段从backstack返回布局时,{{1}被称为。

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

但是,以下代码中没有发生这种情况;谁能解释为什么?我已经确认正在调用EditText' FragmentA

enter image description here

MainActivity.java

onDestroyView()

FragmentA.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);
    }
}

FragmentB.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.");
    }
}

activity_main.xml中

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

fragment_a.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_b.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>

2 个答案:

答案 0 :(得分:18)

后堆栈中的碎片将其状态保留在Bundle中。这包括视图层次结构状态,当前内容为EditText s等。

答案 1 :(得分:-3)

摧毁碎片..使用此onDestroyView  或在onDetach

     if (fragment != null) {

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