Home > Software design >  BottomSheet is not hiding when navigating between different fragments
BottomSheet is not hiding when navigating between different fragments

Time:10-16

Hello there I have a bottomsheet with some textViews (as button) when pressed it navigates to a diffrent fragment but the issue is when textView is pressed and navigated to the fragment still the bottom sheet is not hiding evantually have to tap on the screen for hiding the bottom sheet , I want that when the fragment is launched the bottom sheet will hide, here is a screen recording of what issue im getting link

Profile_Fragment.java

 ImageView accountSettings = view.findViewById(R.id.account_Settings);
  accountSettings.setOnClickListener(
                v -> {
                    BottomSheet bottomSheet = new BottomSheet();
                    bottomSheet.show(requireActivity().getSupportFragmentManager(), bottomSheet.getTag());
                }
        );

BottomSheet.java

public class BottomSheet extends BottomSheetDialogFragment {

    public BottomSheet() {
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_sheet_profile, container, false);
       

        TextView settingsProfileTextView = view.findViewById(R.id.settings);
        settingsProfileTextView.setOnClickListener(v -> {
            Fragment settings_profile = new Settings_Profile();
            FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, settings_profile);
            transaction.addToBackStack(String.valueOf(settings_profile));
            transaction.commit();
          
        });
        TextView editProfileTextView = view.findViewById(R.id.edit_profile);
        editProfileTextView.setOnClickListener(v -> {
            Fragment edit_profile = new Edit_Profile();
            FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, edit_profile);
            transaction.addToBackStack(String.valueOf(edit_profile));
            transaction.commit();
        });
        return view;
    }
}

Edit_Profile.java // the fragment which is being open when textView is pressed

  @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_profile, container, false);
        profilePhoto = view.findViewById(R.id.circleImageView);
        initImageLoader();
        setProfileImage();
        ImageView imageView = view.findViewById(R.id.backArrow);
        imageView.setOnClickListener(v -> {
            Fragment newCase = new Profile_Fragment();
            assert getFragmentManager() != null;
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, newCase);
            transaction.disallowAddToBackStack();
            transaction.commit();
        });

        return view;
    }

CodePudding user response:

Your BottomSheet is hosted by a BottomSheetDialogFragment. Internally it has a Dialog that can be dismissed using dismiss() which will eventually dismiss the BottomSheet.

Applying that in your code whenever a fragment transaction is committed:

    settingsProfileTextView.setOnClickListener(v -> {
        Fragment settings_profile = new Settings_Profile();
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, settings_profile);
        transaction.addToBackStack(String.valueOf(settings_profile));
        transaction.commit();
        dismiss(); // Dismiss the Bottom sheet
      
    });
    TextView editProfileTextView = view.findViewById(R.id.edit_profile);
    editProfileTextView.setOnClickListener(v -> {
        Fragment edit_profile = new Edit_Profile();
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, edit_profile);
        transaction.addToBackStack(String.valueOf(edit_profile));
        transaction.commit();
        dismiss(); // Dismiss the Bottom sheet
    });
  • Related