Skip to content
This repository was archived by the owner on Dec 17, 2020. It is now read-only.

Commit e684398

Browse files
committed
More documentation updates
1 parent 37f2133 commit e684398

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

DOCUMENTATION.md

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
11
# 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.
23

3-
**Index**
4+
## Index
45

56
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)
611
2. [How it works](#2-how-it-works)
712
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)
18+
6. [TaskExecutor & Executor](#6-taskexecutor-executor)
19+
7. [Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat](#7-using-the-taskmanagerlifecycleproxy-to-mimic-the-taskactivitycompat)
820
4. [FAQ](#4-faq)
921

1022

1123
## 1. Basic usage
1224
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:
1325

14-
**Annotation based**
1526

1627
1. Create an implementation of the `Task` class;
1728
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**
2229

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**
2731

28-
> Step 1 and 2 are the same in both styles: annotation and listener based.
32+
<ol start="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>
2936

3037

31-
### 1.1 Creating the task
38+
**Listener based**
39+
40+
<ol start="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>
3244

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.
3445

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>
3549
```java
3650
private class ExampleTask extends Task<Integer, String> {
3751

@@ -78,8 +92,12 @@ public class Main extends TaskActivityCompat implements Task.Callback {
7892

7993
@Override
8094
public Task.Callback onPreAttach(Task<?, ?> task) {
81-
//Restore the user-interface based on the tasks state
82-
return this; //This Activity implements Task.Callback
95+
if("activity-unique-tag".equals(task.getTag())) {
96+
//Restore the user-interface based on the tasks state
97+
return this; //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+
return null;
83101
}
84102

85103
@Override
@@ -94,20 +112,17 @@ public class Main extends TaskActivityCompat implements Task.Callback {
94112
}
95113
```
96114

97-
**Annotation based**
115+
**Annotation based**
98116

99117
```java
100118
public class Main extends TaskActivityCompat {
101119

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-
public void onFinish(ExampleTask task){
107-
120+
@TaskPreExecute("activity-unique-tag")
121+
public void onStart(ExampleTask task){
122+
// Task started
108123
}
109124

110-
@TaskCancel("task-id")
125+
@TaskPostExecute("activity-unique-tag")
111126
public void onFinish(ExampleTask task){
112127
// Task finished
113128
}
@@ -117,7 +132,7 @@ public class Main extends TaskActivityCompat {
117132
> **In-depth:**
118133
> 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()`.
119134
120-
### 1.4 Executing the Task
135+
### 1.4 Executing the task
121136
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.
122137

123138
**Listener based**
@@ -186,10 +201,10 @@ The `Task` and `Callback` class have these methods in common, except for the `do
186201
---
187202
Besides the basics there are some more advanced API's you will probably need.
188203

189-
####**Getting task results**
204+
####**1. Getting the task result**
190205
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.
191206

192-
####**Getting the tasks current state**
207+
####**2. Getting the task state**
193208
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:
194209

195210
* `isFinished()`
@@ -198,13 +213,16 @@ The Android `AsyncTask` API provides the `AsyncTask.getStatus()` method which re
198213
* `isResultDelivered()`
199214
* `isCanceled()`
200215

201-
####**Getting the tasks last progress update**
216+
####**3. Getting the task last progress update**
202217
To get the tasks most recent progress update use the `getLastKnownProgress()` method, this method returns null when no last know progress is available.
203218

204-
####**AdvancedCallback**
219+
####**4. AdvancedCallback**
205220
If you need the `onProgressUpdated` and `onCanceled` callback methods you can implement the `AdvancedCallback` interface, which is an extension of the `Callback` interface.
206221

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**
208226
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.
209227

210228
```java
@@ -224,7 +242,7 @@ You can also use a custom java `Executor` to execute tasks with:
224242
TaskExecutor.executeOnExecutor(new ExampleTask(), yourExecutor);
225243
```
226244

227-
####**Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat**
245+
####**7. Using the TaskManagerLifeCycleProxy to mimic the TaskActivityCompat**
228246
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.
229247

230248
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.

library/src/main/java/org/neotech/library/retainabletasks/providers/TaskActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public final TaskManager getTaskManager() {
6565
return proxy.getTaskManager();
6666
}
6767

68+
public final void bindTaskTarget(Object object){
69+
proxy.bindTaskTarget(object);
70+
}
71+
6872
@Override
6973
public Task.Callback onPreAttach(@NonNull Task<?, ?> task) {
7074
return null;

library/src/main/java/org/neotech/library/retainabletasks/providers/TaskFragment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public final TaskManager getTaskManager(){
6666
return proxy.getTaskManager();
6767
}
6868

69+
public final void bindTaskTarget(Object object){
70+
proxy.bindTaskTarget(object);
71+
}
72+
6973
@Override
7074
public Task.Callback onPreAttach(@NonNull Task<?, ?> task) {
7175
return null;

library/src/main/java/org/neotech/library/retainabletasks/providers/TaskFragmentCompat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public final TaskManager getTaskManager(){
6262
return proxy.getTaskManager();
6363
}
6464

65+
public final void bindTaskTarget(Object object){
66+
proxy.bindTaskTarget(object);
67+
}
68+
6569
@Override
6670
public Task.Callback onPreAttach(@NonNull Task<?, ?> task) {
6771
return null;

0 commit comments

Comments
 (0)