更换碎片时关闭当前碎片

时间:2016-02-26 00:54:16

标签: android android-fragments

下面这段代码在单击html链接时打开一个新片段,我想关闭当前片段,以便它不会在后台保持打开状态。

  protected boolean isURLMatching(String url) {
        url = "http://bakara2.com";
        // some logic to match the URL would be safe to have here
        return true;
    }

    protected void openNextActivity() {
        Fragment fragment = null;
        fragment = new bakara2();
        replaceFragment(fragment);
    }
    public void replaceFragment(Fragment someFragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, someFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

3 个答案:

答案 0 :(得分:0)

getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

答案 1 :(得分:0)

尝试这样做一次!

 getActivity().getFragmentManager().popBackStack();

答案 2 :(得分:0)

@Garry popBackStack()因为@Jusna Leema添加片段而无法正常工作:

transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();

<强> transaction.addToBackStack(空);

简单的删除片段应该可以工作。