How can I be notified when a Snackbar has dismissed itself?

梦想与她 提交于 2019-11-27 17:56:42
and_dev

Google design library supports Snackbar callbacks in version 23. See Snackbar docs and Callback docs. You will then get notified when the Snackbar gets dismissed (and also when shown) and also the type of dismissal if this is useful for you:

snackbar.addCallback(new Snackbar.Callback() {

    @Override
    public void onDismissed(Snackbar snackbar, int event) {
      //see Snackbar.Callback docs for event details
      ...  
    }

    @Override
    public void onShown(Snackbar snackbar) {
       ...
    }
  });
    snackbar.setCallback(new Snackbar.Callback() {

        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
                // Snackbar closed on its own
            }
        }

        @Override
        public void onShown(Snackbar snackbar) {
            ...
        }
    });

Recently I stumbled upon this matter myself when for scrolling and showing Snackback, too many were shown before the first even disappeared. I had to find a way to know if the app should have laid down the Snackbar.

I personally found this solution.

It is true Snackbar itself does not offer any kind of listener for it's state/visibility, but you can still get the View Object out of Snackbar ( getView(); ). From the View Object you have the chance to use a wide variety of methods to add listeners.

To implement it you have to go out of the common "all-in-one-line" Toast/Snackbar usage, because adding listeners returns void .

I personally found OnAttachStateChangeListener to fulfill my needs.

Dropping a snipper with my code in case it might turn useful for you.

Snackbar snack = Snackbar.make(getView(), "My Placeholder Text", Snackbar.LENGTH_LONG);

snack.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
    @Override
        public void onViewAttachedToWindow(View v) {
            canDisplaySnackbar = false;
        }

    @Override
    public void onViewDetachedFromWindow(View v) {

        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                canDisplaySnackbar = true;

                }
        }, 1000);
    }
});
snack.show();

Please note that this is just my implementation for my own problem, Handler with a postDelayed Runnable might not even fit your case. It was just to give a general idea of the implementation I suggested using a snippet I already own.

To be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via setCallback(Callback).

Snackbar.addCallback in kotlin

val snackBar = Snackbar
                .make(view, "Text Snackbar", Snackbar.LENGTH_LONG)
                .addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
                    override fun onShown(transientBottomBar: Snackbar?) {
                        super.onShown(transientBottomBar)
                    }

                    override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
                        super.onDismissed(transientBottomBar, event)
                    }
                })

        val snackBarView = snackBar.view
        snackBarView.setBackgroundColor(Color.RED)
        snackBar.show()

onDismissed is also called when the action Text is clicked for that reason need to put one condition like

event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT

And now new code is look like below.

final Snackbar snackBar = Snackbar.make(findViewById(R.id.root_layout), result, Snackbar.LENGTH_LONG);

snackbar.addCallback(new Snackbar.Callback() {

@Override
public void onDismissed(Snackbar snackbar, int event) {
 if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
        // Snackbar closed on its own
    }  
}

@Override
public void onShown(Snackbar snackbar) {
...
}
});
snackBar.show();
BrentM

There is currently no way to get notification when the Snackbar has finished displaying.

In this thread a workaround is discussed based on a timer for the duration of the Snackbar display. Snackbar in Support Library doesn't include OnDismissListener()?

One issue to consider with this workaround is it is possible that the Snackbar duration is restarted. The material design specification for Snackbar says this will happen if an unrelated dialog or popup is displayed.

Currently you can't achieve it.

There isn't a listener called when the snackbar is dimissed.

The easiest way to do that is to temporarily save the record elsewhere (even a local variable), then re-insert it if they happen to hit the undo button.

    snackbar.addCallback(new Snackbar.Callback() {
        public void onShown(Snackbar snackbar) {
           //  on show  
        }
 public void onDismissed(Snackbar snackbar, int event) {
          //  on dismiss  
        }
      });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!