addToBackStack没有导航回正确的片段



你好,我试图在EditProfile Fragment中包含一个backback,当按下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();
});

我在这篇博客文章中找到了一个很好的解释。你可以试试这个代码,也可以阅读更多关于它的信息

public void addSubscreen(Fragment fragment) {
getSupportManager()
.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
subscreensOnTheStack++;
}

public void popOffSubscreens() {
while (subscreensOnTheStack > 0) {
fragments.popBackStackImmediate();
}
}

最新更新