简体   繁体   中英

Snackbar callback onDismiss() called first

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM