In a snackbar action, how can I be sure it's safe to permanently delete a soft-deleted record from the database?

后端 未结 5 1261
轻奢々
轻奢々 2021-02-04 02:29

I am using Snackbar in android and I have implemented an action so that user can undo the action (the action is clearing all the items in the listview).Removing and adding the i

5条回答
  •  鱼传尺愫
    2021-02-04 03:10

    Example:

    final java.util.Timer timer = new Timer();
    Snackbar snackbar = Snackbar.make(...).setAction("Undo", new OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                for(Word word:temp)
                    data.add(word);
                adapter.notifyDataSetChanged(); 
            }
        }).show();
    timer.schedule(new TimerTask() {
        public void run() {
            // delete from db
        }
    }, snackbar.getDuration());
    

    It may be a good idea to add a little to the snackbar.getDuration() time (100-200ms?) as timers are not very exact in terms of timing and this way they may get called just before the snackbar is about to close, althought the possibility is rather small in this case.

提交回复
热议问题