如何使用按钮像后退按钮来关闭当前片段? [英] How to close the current fragment by using Button like the back button?

查看:181
本文介绍了如何使用按钮像后退按钮来关闭当前片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用的ImageButton关闭当前的片段。

I have try to close the current fragment by using Imagebutton.

我在片段-A,它会转向片段-B当我点击该按钮。

I am in Fragment-A and it will turn to the Fragment-B when I click the button.

当我按一下按钮,在片段-B,它会转向片段-C和关闭的片段-B。

And when I click the button at Fragment-B , it will turn to the Fragment-C and close the Fragment-B.

如果我点击后退按钮片段-C,它会回的片段-A。

If I click the back button at Fragment-C , it will back to the Fragment-A.

在code我已经尝试像以下

The code I have try is like the following

camera_album = (ImageButton) view.findViewById(R.id.camera_album);

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

                            closefragment();
                Fragment fragment = FileBrowserFragment.newInstance(null, null, null) ;
                MainActivity.addFragment(LocalFileBrowserFragment.this, fragment) ;


            }
        });

    private void closefragment() {
        getActivity().getFragmentManager().beginTransaction().remove(this).commit();
    }

当我点击的片段-B后退按钮,它转向片段-C。

When I click the back button at fragment-B , it turn to the Fragment-C.

但是,当我点击片段-C后退按钮,它不回片段-A。 它回到空背景。如果我想回片段-A,我不得不再次单击后退按钮。

But when I click the back button on Fragment-C , it doesn't back to the Fragment-A. It back to the empty background. If I want to back to Fragment-A , I have to click the back button once again.

因此​​,它似乎并没有关闭当前片段完整。

SO , it seem doesn't close the current fragment complete.

如何完成当前的片段像Android这样的后退按钮?

How to finish the current fragment like the back button of Android ?

推荐答案

从片段A,去B,为B代替A和使用 addToBackstack()提交()

From Fragment A, to go to B, replace A with B and use addToBackstack() before commit().

现在从片段B,去到C,第一次使用 popBackStackImmediate(),这将带回答:现在用C代替A,就像第一次交易。

Now From Fragment B, to go to C, first use popBackStackImmediate(), this will bring back A. Now replace A with C, just like the first transaction.

这篇关于如何使用按钮像后退按钮来关闭当前片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆