How to Be Notified When a Snackbar Has Dismissed Itself

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

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

How can I check the dismiss reason with an Angular Material snackbar?

When you subscribe to the afterDismissed event you should be able to get whether that event came from the snack bar action or not.

So for example if you open your snackbar:

const snackBarRef = this.snackBar.open('Dummy message', 'Undo', {duration: 5000});

Then subscribe to the event:

snackBarRef.afterDismissed().subscribe(info => {
if (info.dismissedByAction === true) {
// your code for handling this goes here
}
});

How to dismiss a Snackbar using its own Action button?

For Java,

The .make method returns a Snackbar object. Save an instance of that object by making it final. Then, in the onClick(), call .dismiss:

final Snackbar snackBar = Snackbar.make(findViewById(android.R.id.content), "Snackbar Message", Snackbar.LENGTH_LONG);

snackBar.setAction("Action Message", new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call your action method here
snackBar.dismiss();
}
});
snackBar.show();

For Kotlin,

        Snackbar.make(
findViewById(android.R.id.content),
"Snackbar Message",
Snackbar.LENGTH_INDEFINITE
).setAction("Action Message") {
// Call action functions here
}.show()

How to learn whether a Snackbar button was pressed?

Have a look at the addCallback function - you can add a BaseCallback with an onDismissed function, where you're provided the reason for the dismissal. DISMISS_EVENT_ACTION means your undo button was clicked, anything else means the snackbar was swiped away, disappeared after a timeout, etc.

So you can do something like this:

Snackbar.make(binding.rv, "Deleted", Snackbar.LENGTH_SHORT)
.setAction("Undo") { // no need to do anything }
.setCallback(object: BaseCallback<Snackbar> {
override fun onDismissed(transientBottomBar: Snackbar, event: Int) {
// if the user didn't click Undo, delete the item
if (event != DISMISS_EVENT_ACTION) adapter.removeItem(cachedPosition)
}
}
.show()

It would still be a good idea to remove the item from the list though, and restore it in the setAction lambda or in the onDismissed callback if the button was pressed. That way the user sees it disappear and reappear - deferring deletion is good for things like avoiding removing things from databases, where you don't want to touch the real data until you're sure.

You could still do that with what you have here:

// delete/hide it
adapter.removeItem(cachedPosition)

Snackbar.make(binding.rv, "Deleted", Snackbar.LENGTH_SHORT)
.setAction("Undo") { // no need to do anything }
.setCallback(object: BaseCallback<Snackbar> {
override fun onDismissed(transientBottomBar: Snackbar, event: Int) {
// add the item back if they DID click Undo
if (event == DISMISS_EVENT_ACTION) adapter.restoreItem(cachedPosition, cachedItem)
else // do something else if they didn't, like delete cachedItem from database
}
}
.show()


Also if you're going to be accessing the adapter and managing the undo state externally like this, I'd make removeItem return the item that was removed (so you don't have to access the adapter's data list directly) and maybe rename restoreItem to addItem since technically you're inserting whatever you like at a position.

Really though, it would be better to keep the last deleted item internal to the adapter, so you can call remove(position) and restore() and let the adapter take care of the details and manage its own state. It's just cleaner and prevents bugs

Snackbar not being dismissed on action click

Try something like this.

mBtn.setOnClickListener {
if (mTextView.text.isEmpty()) {
mSnackbar.show()
} else {
mSnackbar.dismiss()
}
}

How to wait to Snackbar ? I want to know when it is closed

Snackbar.make(view, "Some text", Snackbar.LENGTH_SHORT)
.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);

startActivity(this, NextActivity.class);
}
}).show();

Xamarin.android how to add snackbar callback

You could define a CallBack first,then use AddCallBack method.

class MySnackCallBack : BaseTransientBottomBar.BaseCallback
{
public override void OnDismissed(Java.Lang.Object transientBottomBar, int e)
{
base.OnDismissed(transientBottomBar, e);

}

public override void OnShown(Java.Lang.Object transientBottomBar)
{
base.OnShown(transientBottomBar);

}
}

Snackbar snackbar = Snackbar.Make(view, message, Snackbar.LengthShort);
snackbar.AddCallback(new MySnackCallBack());
snackbar.Show();


Related Topics



Leave a reply



Submit