Search code examples
androidandroid-room

How to undo a deletion made using Room persistent library?


I am not sure if this is a qualified question .

I am using snackbar feature to undo a deletion and am really confused how to do it . I am using room library and after deletion ,the id associated with my data is also deleted so i cannot refer to the deleted id to insert the data at that specific id. When I click on undo button I want my data to be restored again to its old position and not as a new insertion.

Correct me if it is not at all possible or provide any alternatives. Thankyou


Solution

  • One way to do it is by using Soft-Delete technique.

    Add a new column and call it status for example flag with default value 0

    Then if its deleted set the flag column to 1

    and the query will only get results where flag = 0

    and for un-deletion you can just set the flag from 1 to 0