fabiendevos / nanotasks

Extremely light way to execute code in the background on Android. Alternative to AsyncTask.
Other
387 stars 36 forks source link

Is there a way to cancel the task? #14

Closed gauravwally closed 7 years ago

Yousefjb commented 8 years ago

Task extends AbstractTasks which extends AsyncTask which has cancel() method
so i guess you can do :

             Task<String> t= new Task<String>(v.getContext(), new BackgroundWork<String>() {
                    @Override
                    public String doInBackground() throws Exception {
                        final int DELAY = 3;
                        Thread.sleep(TimeUnit.SECONDS.toMillis(DELAY));
                        return "Worked hard for " + DELAY + " seconds";
                    }
                }, new Completion<String>() {
                    @Override
                    public void onSuccess(Context context, String result) {
                        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
                        hideProgress();
                    }

                    @Override
                    public void onError(Context context, Exception e) {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                        hideProgress();
                    }
                });

and then you can do either :

t.execute();
t.cancel();
fabiendevos commented 7 years ago

What @Yousefjb said. :)