You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 17, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: DOCUMENTATION.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,10 +170,10 @@ public class Main extends TaskActivityCompat implements Task.Callback {
170
170
---
171
171
Besides the basics there are some more advanced API's you will probably need.
172
172
173
-
####**1. Getting the task result**
173
+
####**1. Getting the task result**
174
174
Unlike the default Android `AsyncTask` implementation you don't get `Task` results as a parameter, instead you will need to call the `Task.getResult()` method, which returns the tasks result.
175
175
176
-
####**2. Getting the task state**
176
+
####**2. Getting the task state**
177
177
The Android `AsyncTask` API provides the `AsyncTask.getStatus()` method which returns an enum value which can be used to determinate the tasks current state. Instead of using that method combined with an enum you can use on of the following methods:
178
178
179
179
*`isFinished()`
@@ -182,16 +182,16 @@ The Android `AsyncTask` API provides the `AsyncTask.getStatus()` method which re
182
182
*`isResultDelivered()`
183
183
*`isCanceled()`
184
184
185
-
####**3. Getting the task last progress update**
185
+
####**3. Getting the task last progress update**
186
186
To get the tasks most recent progress update use the `getLastKnownProgress()` method, this method returns null when no last know progress is available.
187
187
188
-
####**4. AdvancedCallback**
188
+
####**4. AdvancedCallback**
189
189
If you need the `onProgressUpdated` and `onCanceled` callback methods you can implement the `AdvancedCallback` interface, which is an extension of the `Callback` interface.
190
190
191
-
####**5. Annotations outside Activities or Fragments**
191
+
####**5. Annotations outside Activities or Fragments**
192
192
By default annotated methods are only resolved if they are added to a `TaskActivityCompat` or `TaskFragmentCompat`, but you can register custom classes using the `TaskActivityCompat.bindTaskTarget()` method you must call this method as soon as possible, for example in the constructor of the Activity to prevent missing callbacks. You obviously need to re-register the object when a configuration change occurs like rotation.
193
193
194
-
####**6. TaskExecutor & Executor**
194
+
####**6. TaskExecutor & Executor**
195
195
You can also execute tasks without using a `TaskManager` this means that you are responsible for removing and setting the `Callback` listener. Executing tasks without using the `TaskManager` is handy when you don't necessarily need to get any feedback to the user-interface.
196
196
197
197
```java
@@ -211,7 +211,7 @@ You can also use a custom java `Executor` to execute tasks with:
####**7. Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat**
214
+
####**7. Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat**
215
215
If you already use some custom Activity or Fragment implementation you might not be able to use the `TaskActivityCompat` or `TaskFragmentCompat` class. To overcome this problem you can implement the behaviour of the `TaskActivityCompat` yourself using the `TaskManagerLifeCycleProxy` class.
216
216
217
217
Create a new `TaskManagerLifeCycleProxy` instance and let your Activity (or Fragment) implement the `TaskManagerOwner` interface. Override the`onStart()` and `onStop()` methods and proxy those together with the `getTaskManager()` method to the `TaskManagerLifeCycleProxy` instance.
@@ -249,7 +249,7 @@ public class MyBaseActivity extends SomeActivity implements TaskManagerOwner {
249
249
## 3. How it works
250
250
How this library works is not extremely complicated it can however be quite difficult to understand correctly if you have limited knowledge about the Android Activity and Fragment life-cycle and how Android manages these objects.
251
251
252
-
####**How are task objects retained?**
252
+
####**How are task objects retained?**
253
253
The first thing to understand is how `Task` objects are kept alive when the Activity is destroyed by the system when a configuration change occurs (like rotation). Often used solutions include using static variables, the `Application` class or the `Activity.onRetainNonConfigurationInstance()` method to store objects in. These methods are however non optimal and Google suggests using the Fragment API in combination with `Fragment.setRetainInstanceState(true)` instead. Using the Fragment API avoids keeping objects alive after the Activity or Fragment is destroyed for good which can happen when the `Application` class or static variables are used. Google demonstrates the practise of using the Fragment API in combination with `setRetainInstanceState(true)` in the excellent [Android Architecture Lifecycle](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.html) library.
254
254
255
255
This library leverages the same principle and uses so called *"no-ui-fragments"* which are retained using `setRetainInstanceState(true)` to store objects in. The objects stored in these Fragments are `TaskManager` objects which in their turn store `Task` objects. The creation of these no-ui-fragments happens as soon as the first call to one of these methods is made:
@@ -263,17 +263,17 @@ This library leverages the same principle and uses so called *"no-ui-fragments"*
263
263
264
264
Essentially any time you request a `TaskManager`. You should however note that the library itself also internally calls these methods.
265
265
266
-
####**What about the Callback listeners?**
266
+
####**What about the Callback listeners?**
267
267
Each `Task` has a listener attached to it (`Callback` interface), these listeners must not leak between Activity instances, especially since a listener might hold a reference to an Activity causing it to leak the Activity object. Therefor the `TaskManager` makes sure listeners must be removed from a task as soon as the Activity is being destroyed. A new listener is attached to the `Task` when the new Activity is created, so that the `Task` can still report it's result. To prevent Tasks from reporting their results before the Activity is started the `Callback` listeners are removed in `onStop()` and attached in `onStart()`.
268
268
269
269
The new listeners that need to be attached to a `Task` are acquired during `onStart()` the Activity (or Fragment) TaskManager will call the `onPreAttach(Task)` method which then should return the new listeners for that specific `Task`. When annotations are used `Callback` listeners are automatically generated at compile time and automatically attached to `Tasks` so there is no need to override `onPreAttach(Task)`.
270
270
271
-
####**What happens when a Task without Callback finishes?**
271
+
####**What happens when a Task without Callback finishes?**
272
272
When a `Task` does not have a `Callback` listener attached to it (after `onStop()` and before `onStart()` is called) it will skip/wait with the delivery and deliver the results as soon as a new listener is attached. This happens right after the `onPreAttach(Task)` method returns, as the `TaskManager` will immediately attach the newly provided `Callback` listener to the `Task` and the `Task` can immediately fire the listener if it was waiting for it. Because this all happens during the Activity or Fragment `onStart()` you need to be sure that at this point the user-interface is ready. If you manually call one of the `TaskManager.attach()` methods a `Task` might also immediately fire the new listener.
273
273
274
274
**Important:** Only the `onPostExecute()` and `onCanceled()` methods will wait for delivery, other method's like `onProgressUpdate` won't wait for delivery and will be skipped if no `Callback` listener is attached to the `Task`. You can restore a tasks progress using the `Task.getLastKnownProgress()` method.
275
275
276
-
####**How does the Task and Callback life-cycle work?**
276
+
####**How does the Task and Callback life-cycle work?**
277
277
A `Task` basically has four life-cycle methods *(its heavily based on Android's AsyncTask)*:
Copy file name to clipboardExpand all lines: README.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,6 +60,15 @@ dependencies {
60
60
}
61
61
```
62
62
63
+
Because of the dependency on Google's new Architecture Lifecyle library you must add Google's maven repository to your top-level build.gradle file (if you did not already do this).
64
+
```groovy
65
+
allprojects {
66
+
repositories {
67
+
maven { url 'https://maven.google.com' }
68
+
}
69
+
}
70
+
```
71
+
63
72
## Why and when
64
73
65
74
*When and why should I use this library?* Always! its awesome! No, obviously you should not always use this library. **This library is useful when you need to do things in the **background** which are heavily bound to the user-interface** like: Refreshing a list which is populated from a database, loading an article from a network source or decoding an image, all those things are quit tightly bound to the user-interface and when the device rotates you don't want to cancel this work or do it again. If you need: task scheduling, automatic retries, task persistence across reboots, task constrains etc. then you need to use an additional library.
0 commit comments