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
+48-30Lines changed: 48 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,51 @@
1
1
# Android-Retainable-Tasks Documentation
2
+
When you are new to this library the advice is to read at least chapter *"1. Basic usage"*. Besides this documentation make sure to try out the sample app and check the source code. If something is not clear to you please don't keep it for yourself but create and issue instead.
2
3
3
-
**Index**
4
+
## Index
4
5
5
6
1.[Basic usage](#1-basic-usage)
7
+
1.[Creating the task](#1-1-creating-the-task)
8
+
2.[Extending from the TaskActivityCompat class](#1-2-extending-from-the-taskactivitycompat-class)
9
+
3.[Callback](#1-3-callback)
10
+
4.[Executing the task](#1-4-executing-the-task)
6
11
2.[How it works](#2-how-it-works)
7
12
3.[Tips and tricks](#3-tips-and-tricks)
13
+
1.[Getting the task result](#1-getting-the-task-result)
14
+
2.[Getting the task state](#2-getting-the-task-state)
15
+
3.[Getting the task last progress update](#3-getting-the-task-last-progress-update)
16
+
4.[AdvancedCallback](#4-advancedcallback)
17
+
5.[Annotations outside Activities or Fragments](#5-annotations-outside-activities-or-fragments)
7.[Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat](#7-using-the-taskmanagerlifecycleproxy-to-mimic-the-taskactivitycompat)
8
20
4.[FAQ](#4-faq)
9
21
10
22
11
23
## 1. Basic usage
12
24
To execute a task (which is bound to the user-interface and is automatically retained across configuration changes) you need to do follow these 4 simple steps:
13
25
14
-
**Annotation based**
15
26
16
27
1. Create an implementation of the `Task` class;
17
28
2. Make your Activity extend the `TaskActivityCompat` class (or for Fragments the `TaskFragmentCompat` class);
18
-
3. Create one or more annotated methods with the following annotations: `@TaskPreExecute`, `@TaskPostExecute`, `@TaskCancel`, `@TaskProgress` and `@TaskAttach`;
19
-
4. Execute the task using the `TaskManager`;
20
-
21
-
**Listener based**
22
29
23
-
1. Create an implementation of the `Task` class;
24
-
2. Make your Activity extend the `TaskActivityCompat` class (or for Fragments the `TaskFragmentCompat` class);
25
-
3. Implement the `Callback` interface somewhere and provide a new callback when the Activity is restarted using `TaskActivityCompat.onPreAttach()`;
26
-
4. Execute the task using the `TaskManager` and point it to your `Callback` implementation;
30
+
**Annotation based**
27
31
28
-
> Step 1 and 2 are the same in both styles: annotation and listener based.
32
+
<olstart="3">
33
+
<li>Create one or more annotated methods with the following annotations: <code>@TaskPreExecute</code>, <code>@TaskPostExecute</code>, <code>@TaskCancel</code>, <code>@TaskProgress</code> and <code>@TaskAttach</code>;</li>
34
+
<li>Execute the task using the <code>TaskManager</code>;</li>
35
+
</ol>
29
36
30
37
31
-
### 1.1 Creating the task
38
+
**Listener based**
39
+
40
+
<olstart="3">
41
+
<li>Implement the <code>Callback</code> interface somewhere and provide a new callback when the Activity is restarted using <code>TaskActivityCompat.onPreAttach()</code>;</li>
42
+
<li>Execute the task using the <code>TaskManager</code> and point it to your <code>Callback</code> implementation;</li>
43
+
</ol>
32
44
33
-
You will need to extend the `Task` class to create a custom task. The `Task` class is heavily based on the default Android `AsyncTask` class and you will need to override at least the `doInBackground` method and provide an appropriate constructor.
34
45
46
+
### 1.1 Creating the task
47
+
48
+
You will need to extend the `Task` class to create a custom task. The `Task` class is heavily based on the default Android </code>
//Restore the user-interface based on the tasks state
97
+
returnthis; //This Activity implements Task.Callback for the given task
98
+
}
99
+
// Other (unknown) tasks, code won't reach this if you execute just one task
100
+
returnnull;
83
101
}
84
102
85
103
@Override
@@ -94,20 +112,17 @@ public class Main extends TaskActivityCompat implements Task.Callback {
94
112
}
95
113
```
96
114
97
-
**Annotation based**
115
+
**Annotation based**
98
116
99
117
```java
100
118
publicclassMainextendsTaskActivityCompat {
101
119
102
-
// Now this is cool, we can have a single method handle both calls,
103
-
// also note how this method will still be called in the new
104
-
// Activity instance after after rotation.
105
-
@TaskPostExecute("task-id")
106
-
publicvoidonFinish(ExampleTasktask){
107
-
120
+
@TaskPreExecute("activity-unique-tag")
121
+
publicvoidonStart(ExampleTasktask){
122
+
// Task started
108
123
}
109
124
110
-
@TaskCancel("task-id")
125
+
@TaskPostExecute("activity-unique-tag")
111
126
publicvoidonFinish(ExampleTasktask){
112
127
// Task finished
113
128
}
@@ -117,7 +132,7 @@ public class Main extends TaskActivityCompat {
117
132
> **In-depth:**
118
133
> The `TaskManger` (or actually a internal class) will detect when the Activity stops (`onStop()`). and will automatically remove all `Callback` listeners when this happens. Removing the listeners is needed to avoid memory leaks, as listeners could reference to Activities or Fragments which are about to be destroyed by the Android system. Removing the listeners in `onStop()` also avoids having tasks report their result while the Activity is in the background. As soon as `onStart()` is called the `TaskManager` takes care of setting new listeners on all the tasks. You will need to provide these new listeners when the `onPreAttach()` method is called. If annotations are used this process is a bit more automated and you won't need to override `onPreAttach()`.
119
134
120
-
### 1.4 Executing the Task
135
+
### 1.4 Executing the task
121
136
Executing a `Task` is extremely easy, just obtain an instance of the `TaskManager` and call one of the `execute()` methods. When working with `Callback` listeners instead of annotations you will need to provide the initial `Callback` listener when executing a `Task`, when working with annotations this is done automatically. Preferably your activity implements the `Callback` interface when working without annotations, but this isn't necessarily needed.
122
137
123
138
**Listener based**
@@ -186,10 +201,10 @@ The `Task` and `Callback` class have these methods in common, except for the `do
186
201
---
187
202
Besides the basics there are some more advanced API's you will probably need.
188
203
189
-
####**Getting task results**
204
+
####**1. Getting the task result**
190
205
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.
191
206
192
-
####**Getting the tasks current state**
207
+
####**2. Getting the task state**
193
208
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:
194
209
195
210
*`isFinished()`
@@ -198,13 +213,16 @@ The Android `AsyncTask` API provides the `AsyncTask.getStatus()` method which re
198
213
*`isResultDelivered()`
199
214
*`isCanceled()`
200
215
201
-
####**Getting the tasks last progress update**
216
+
####**3. Getting the task last progress update**
202
217
To get the tasks most recent progress update use the `getLastKnownProgress()` method, this method returns null when no last know progress is available.
203
218
204
-
####**AdvancedCallback**
219
+
####**4. AdvancedCallback**
205
220
If you need the `onProgressUpdated` and `onCanceled` callback methods you can implement the `AdvancedCallback` interface, which is an extension of the `Callback` interface.
206
221
207
-
####**TaskExecutor & Executor**
222
+
####**5. Annotations outside Activities or Fragments**
223
+
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.
224
+
225
+
####**6. TaskExecutor & Executor**
208
226
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.
209
227
210
228
```java
@@ -224,7 +242,7 @@ You can also use a custom java `Executor` to execute tasks with:
####**Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat**
245
+
####**7. Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat**
228
246
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.
229
247
230
248
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.
0 commit comments