Snackbar callback onDismiss() called first

1.9k views Asked by At

I have defined the call back for handling snackbar dismiss() and show().

   Snackbar snackbar = Snackbar.make(rootView, message, Snackbar.LENGTH_LONG);
   View snackBarView = snackbar.getView();
   snackBarView.setBackgroundColor(Color.argb(255, 8, 20, 37));
   snackbar.setCallback(new Snackbar.Callback() {
                @Override
                public void onShown(Snackbar snackbar) {
                    super.onShown(snackbar);
                    Debug.e(TAG, "Shown");
                }

                @Override
                public void onDismissed(Snackbar snackbar, int event) {
                    super.onDismissed(snackbar, event);
                    Debug.e(TAG, "Dismissed");
                }
            });
            snackbar.show();

But when I check my Logcat the onDismiss() is calling before onShown()

 12-31 12:36:29.601 2883-2883/BaseFragment: Dismissed
 12-31 12:36:29.880 2883-2883/BaseFragment:    Shown

So am I doing something wrong? Any Idea?

1

There are 1 answers

0
Shahood ul Hassan On

It might be because another snack bar is replacing this one. In such a case, onDismissed() will be called for the earlier one and onShown() for the new one. In your logs, you should also see which snack bar each method is being called for. You may also check the event for which onDismissed() is called. If it is 4, it means it has been dismissed due to being replaced with another snack bar. Hope it helps!