尽管片段替换有效,但我的片段并未从片段容器中删除

问题描述 投票:0回答:2

我的代码在

MainActivity
中包含一个名为
changeFragment()
的方法,我用它来替换和删除两个片段容器中的片段。当我尝试使用此方法将容器中的片段替换为另一个片段时,它有效,但当我尝试删除片段时,它不起作用。 Logcat 中没有显示错误,并且在调用此方法后代码继续,但即使我无法再与片段中的按钮交互,片段的视觉效果也不会被删除。

这是我的

changeFragment()
方法:

public void changeFragment(String fragmentContainer, String fragmentRequested) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    if (Objects.equals(fragmentRequested, "None")) {
        // Find the fragment to remove
        Fragment fragmentToRemove;
        if (Objects.equals(fragmentContainer, "menu")) {
            fragmentToRemove = fragmentManager.findFragmentById(R.id.menucontainer);
        } else {
            fragmentToRemove = fragmentManager.findFragmentById(R.id.selectioncontainer);
        }

        if (fragmentToRemove != null) {
            transaction.remove(fragmentToRemove);
        }


        // If a fragment replacement is requested:
    } else {
        // Find the new fragment to replace the old one with
        Fragment fragmentToBeDisplayed = switch (fragmentRequested) {
            case "CardNumberSelection" -> FragmentCardNumberSelection.newInstance();
            case "GameCreation" -> FragmentGameCreation.newInstance();
            case "Guide" -> FragmentGuide.newInstance();
            case "PlayerSelection" -> FragmentPlayerSelection.newInstance();
            case "Settings" -> FragmentSettings.newInstance();
            case "TitlePage" -> FragmentTitlePage.newInstance();
            default -> null;
        };

        // Replace the old fragment with the new one
        assert fragmentToBeDisplayed != null;
        if (Objects.equals(fragmentContainer, "menu")) {
            transaction.replace(R.id.menucontainer, fragmentToBeDisplayed);
        } else {
            transaction.replace(R.id.selectioncontainer, fragmentToBeDisplayed);
        }
    }

    transaction.commit();
    if (fragmentManager.findFragmentById(R.id.menucontainer) == null) {
        findViewById(R.id.settingsmenu).setVisibility(View.VISIBLE);
        findViewById(R.id.guidemenu).setVisibility(View.VISIBLE);
    } else {
        findViewById(R.id.settingsmenu).setVisibility(View.INVISIBLE);
        findViewById(R.id.guidemenu).setVisibility(View.INVISIBLE);
    }
}

以下代码旨在从片段容器 (

FragmentGameCreation
) 中删除旧片段 (
menucontainer
),但事实并非如此。这行代码位于MainActivity中的一个方法中:

changeFragment("menu","None");

这是我用来显示

FragmentGameCreation
的代码,当我单击标题页片段中的按钮时,它成功地将我的
FragmentTitlePage
替换为
FragmentGameCreation
。这段代码在我的
FragmentTitlePage
类中:

// Start Game
startGameButton.setOnClickListener(view -> {
   // Replace the title page fragment with the game creation fragment
   FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
   fragmentManager.beginTransaction().replace(R.id.menucontainer, frag, "menuTag").commit();
});

我已使用调试器确保所有预期的代码行都正在运行,而且全部都在运行。

我还尝试过在将

FragmentGameCreation
放入menucontainer之前全局创建新实例,并使用
dindFragmentByTag()
方法代替
findFragmentByID()
,但都没有解决这个问题。

java android android-fragments
2个回答
0
投票

尝试添加transaction.addToBackStack(null)以确保片段完全移除

if (fragmentToRemove != null) {
    transaction.remove(fragmentToRemove);
    transaction.addToBackStack(null); 
}

0
投票

该错误是由应用程序中稍后的代码引起的,由设计用于在退出之前等待按下按钮的 while 循环引起。删除这个 while 循环解决了问题,并让片段按预期删除。

© www.soinside.com 2019 - 2024. All rights reserved.