【问题标题】:addToBackStack is not navigating back to the correct fragmentaddToBackStack 没有导航回正确的片段
【发布时间】:2021-09-19 12:36:15
【问题描述】:

你好,我试图在 EditProfile Fragment 中包含一个 backstack,当按下 back 时它会返回到 Profile Fragment 但它会返回到 Home Fragment

如果您想要更多参考代码,请告诉我,我会更新 完整代码的问题

个人资料片段

editProfileButton = relativeLayout.findViewById(R.id.edit_profile_button); // buuton to start editprofile fragment 
        editProfileButton.setOnClickListener(v -> {
            Fragment edit_profile = new Edit_Profile();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, edit_profile);
            transaction.addToBackStack(String.valueOf(new Profile_Fragment())); // i thinked this method of implementing string.valueof will navigate back to the given fragment but as you know it is not working
            transaction.commit();
        });

【问题讨论】:

    标签: android fragment-backstack android-fragment-manager


    【解决方案1】:

    我在this 博文中找到了一个很好的解释。您可以尝试此代码并阅读更多相关信息。

    public void addSubscreen(Fragment fragment) {
        getSupportManager()
            .beginTransaction()
            .replace(R.id.container, fragment)
            .addToBackStack(null)
            .commit();
        subscreensOnTheStack++;
    }
    
    
    public void popOffSubscreens() {
        while (subscreensOnTheStack > 0) {
            fragments.popBackStackImmediate();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多