0

hello im trying to include a backstack to EditProfile Fragment that when back is pressed it goes back to Profile Fragment but it goes back to the Home Fragment

if you want more refrence of the code please tell me i will update the question with full code

Profile 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();
        });
Vasant Raval
  • 257
  • 1
  • 12
  • 31

1 Answers1

0

I found a good explanation in this blog post. You can try this code and also read more about it.

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


public void popOffSubscreens() {
    while (subscreensOnTheStack > 0) {
        fragments.popBackStackImmediate();
    }
}
Eishon
  • 1,274
  • 1
  • 9
  • 17