Android片段popBackStack并替换不删除视图

时间:2014-02-28 21:42:03

标签: android android-fragments

我正在努力学习使用片段。

假设我有一个活动,包含2个片段 - FragmentAFragmentB

在我的活动中,我添加了片段,当我按下后退按钮时,我认为能够删除片段视图:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        mFragmentA = new FragmentA();
        fragmentTransaction.add(R.id.fragment_container, mFragmentA).addToBackStack("fragA").commit();
    }

在FragmentA的视图中,我有2个按钮。一个据说可以回去,一个用FragmentB替换FragmentA。

所以这就是FragmentA的样子:

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

        View view = inflater.inflate(R.layout.fragment_a_layout, null);

        Button backButton = (Button) view.findViewById(R.id.frag_a_back);
        backButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack("fragA", FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
        });

        Button nextButton = (Button) view.findViewById(R.id.frag_a_next);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();

                FragmentB fragB = new FragmentB();

                ft.replace(R.id.fragment_container, fragB).commit();
            }
        });

        container.addView(view);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

问题: 当我去fragmentB或者按下后退按钮时,为什么FragmentA的两个按钮仍然显示在屏幕上?我想要后退按钮删除FragmentA。片段是否被删除/分离,但视图不是?

(我正在使用FrameLayout处理所有内容,我仍然可以看到FragmentA视图)

编辑 =============================

好吧,我意识到有onDestroyView()方法,但我不确定这是否是删除FragmentA视图的正确方法?

这需要我坚持父视图的实例,并将参考var保存到我的片段布局视图中;这样我可以使用父视图删除所有视图

1 个答案:

答案 0 :(得分:5)

您无需明确执行container.addView(view);。只需返回膨胀的视图。以下更改可能有所帮助。

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

        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_a_layout, null);

        Button backButton = (Button) view.findViewById(R.id.frag_a_back);
        backButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack("fragA", FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
        });

        Button nextButton = (Button) view.findViewById(R.id.frag_a_next);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();

                FragmentB fragB = new FragmentB();

                ft.replace(R.id.fragment_container, fragB).commit();
            }
        });

        //container.addView(view);

        return view;
    }