-1

So this thing is troubling me since past few days now. I have a dialog in my PostAdapter class. When someone clicks on 'more' menu, a dialog (which appears as a bottom sheet) appears. There is no issue in showing the dialog. It is working perfectly fine. But the issue is after clicking on 'more', if I perform any other action, the dialog opens several times automatically. I have to tap on the screen that many times to dismiss each opened dialog. I have dismissed the dialog in the code but the behaviour is quite unexpected. The dialog keeps opening everytime I perform some action with the post even after switching between fragments and coming back to Home Fragment where all the posts are displayed.

Its very strange because the dialog should only open on clicking 'more' and not on any other action. I have tried solutions given in some similar questions but none of them have worked.

viewHolder.binding.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirebaseDatabase.getInstance().getReference()
                            .child("Posts")
                            .child(post.getPostId())
                            .addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot snapshot) {
                                    if (snapshot.exists()) {
                                        String postedBy = snapshot.child("postedBy").getValue().toString();
                                        if (FirebaseAuth.getInstance().getCurrentUser().getUid().equals(postedBy)) {
                                            showBottomSheetDialog(post, holder);
                                        } else {
                                            showBottomSheetDialogForOthersPosts(post, holder);
                                        }
                                    }
                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError error) {

                                }
                            });
                }
            });

    private void showBottomSheetDialogForOthersPosts(Post post, RecyclerView.ViewHolder holder) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.bottomsheetlayout_othersposts);

        LinearLayout share = dialog.findViewById(R.id.share);
        LinearLayout report = dialog.findViewById(R.id.report);

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                Toast.makeText(context, "Share is clicked", Toast.LENGTH_SHORT).show();
            }
        });

        report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                Toast.makeText(context,"Report is Clicked",Toast.LENGTH_SHORT).show();

            }
        });

        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.BottomSheetDialogAnimation;
        dialog.getWindow().setGravity(Gravity.BOTTOM);
    }

1 Answers1

0

You must be getting multiple callbacks in

public void onDataChange(@NonNull DataSnapshot snapshot) {}

I will suggest you to declare dialog globally and check whether that dialog is already visible or not, if not then only show the dialog.

final Dialog dialog = new Dialog(context);
Parmesh
  • 480
  • 4
  • 11
  • I didn't really understood what you meant by this line "declare dialog globally and check whether that dialog is already visible or not, if not then only show the dialog" but if that meant to write "final Dialog dialog = new Dialog(context);" on the top where we declare variables, then I did that and it didn't work. Sorry if I am wrong. – Krishna Jindal Nov 21 '21 at 07:20