You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跳转不同Fragment时BottomSheet不自动隐藏的问题如何解决?

解决方案

核心原因是跳转Fragment前没有主动调用BottomSheet的关闭方法,手动调用dismiss()即可解决问题。
你只需要修改BottomSheet.java里两个TextView的点击逻辑,在Fragment事务提交前添加关闭代码即可,修改后的代码如下:

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 -> {
            // 关闭当前BottomSheet
            dismiss();
            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 -> {
            // 关闭当前BottomSheet
            dismiss();
            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;
    }
}

如果你的使用场景中可能出现Fragment状态已保存后再触发点击的情况,为了避免抛出状态丢失异常,可以把dismiss()替换为dismissAllowingStateLoss()。

内容的提问来源于stack exchange,提问作者Vasant Raval

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:18:04