I have a RecyclerView
and have an undo-functionality, where you delete an item but can press undo on the appearing Snackbar
to insert the item back into the list.
Now unfortunately, this happens:
When deleting elements quickly in a row, some of them are being restored while deleting another. It seems like creating a second Snackbar
too quickly after the first triggers the Action
, e.g. in this case the undo.
Could this be the case, or is it my code that is faulty?
The code isn't too complex:
private void deleteSATemp(int position) {
SelfAffirmation saToDelete = selfAffirmations.get(position);
String content = saToDelete.getContent();
String partOfContent = content.substring(0, 20);
selfAffirmations.remove(position);
notifyItemRemoved(position);
createUndoSnackbar(saToDelete, position, partOfContent);
}
This is the "fake" removal part, where the element is removed from the list visually but not from the database yet, in case of an undo action.
And here is the Snackbar
part:
private void createUndoSnackbar(SelfAffirmation saToDelete, int position, String pieceOfSAContent) {
Snackbar snackbar = Snackbar.make(((Activity) mContext).findViewById(R.id.sa_activity_parent_layout), mContext.getString(R.string.snackbarItemDeletedPlaceholder, pieceOfSAContent),
5000);
snackbar.setAction(R.string.snackbarUndoText, view -> {
selfAffirmations.add(position, saToDelete);
notifyItemInserted(position);
});
snackbar.addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {
mCallbackListener.onItemDeleted(saToDelete);
}
}
});
snackbar.show();
}
The database works with LiveData
. I can't imagine that this Android feature would be implemented so carelessly, so it has to be my code.