My use case is simple. Upload multiple images in the background and show the progress in a notification. Now I want to cancel this task when the user presses the cancel button in the notification. As the upload task cannot be serialized then how am I supposed to cancel that particular upload task?
Asked
Active
Viewed 454 times
0
1 Answers
3
Upload multiple images in the background and show the progress in a notification.
For those operations, I assume you are using a line of code similar to this:
UploadTask uploadTask = storageReference.putFile(filePath);
Now I want to cancel this task when the user presses the cancel button in the notification.
As seen the above line of code, the putFile(Uri uri) method that is called on a StorageReference object, returns an object of type UploadTask. This class is a subclass of StorageTask, which in terms is a subclass of ControllableTask, which is also a subclass of CancellableTask.
Because of the inheritance relationship between these classes, you can simply call CancellableTask's cancel() method:
Attempts to cancel the task.
In order to cancel the Task. In code should look like this:
uploadTask.cancel();

Alex Mamo
- 130,605
- 17
- 163
- 193
-
Thanks Alex. But that is what i am concerned about. How will i get the instance of this particular uploadTask that I need to cancel. I need to pass it via intent. Somewhat like this: `intent.putExtra("task", (Parcelable) uploadTask);`. But uploadTask is **not Parcelable** nor Serializable. @Alex_Mamo – User Dec 04 '21 at 09:55
-
As you already mentioned, you cannot pass an instance of UploadTask via Intent, since it's not Parcelable nor Serializable. What you can do, is to make it [static](https://stackoverflow.com/questions/11733361/how-to-pass-reference-non-serializable-from-one-activity-to-another), or convert that object to a [Parcelable](https://stackoverflow.com/questions/33012615/how-to-pass-non-parcelable-objects-to-from-activity-to-another-activity) one, right? – Alex Mamo Dec 04 '21 at 10:01