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);
}