|
1 | 1 | # Android-Retainable-Tasks |
2 | | -Asynchronous task micro-library based on the Android AsyncTask |
| 2 | +An easy to use micro-library for easy asynchronous background tasking with callbacks to the UI. This library is based on the Android AsyncTask implementation but with support for configuration changes (orientation) and callbacks to the UI. It also support custom Executors. |
| 3 | + |
| 4 | +Usage |
| 5 | +-------- |
| 6 | +Extend the `Task` class to build a custom task. The `Task` class is heavily based on the default Android `AsyncTask` class and you will need to override the `doInBackground` method. Note that the `Task` class doesn't come with a generic type for input parameters, you should provide input when constructing the `Task` instance (using the constructor for example). |
| 7 | + |
| 8 | +```java |
| 9 | +private class ExampleTask extends Task<Integer, String> { |
| 10 | + |
| 11 | + protected String doInBackground() { |
| 12 | + for(int i = 0; i < 100; i++) { |
| 13 | + if(isCancelled()){ |
| 14 | + break; |
| 15 | + } |
| 16 | + SystemClock.sleep(50); |
| 17 | + publishProgress(i); |
| 18 | + } |
| 19 | + return "Result"; |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +Then execute the task using the TaskExecutor. You can also execute tasks using a custom Executor. |
| 25 | + |
| 26 | +```java |
| 27 | +TaskExecutor.executeParallel(new ExampleTask()); |
| 28 | +``` |
| 29 | + |
| 30 | +```java |
| 31 | +TaskExecutor.executeSerial(new ExampleTask()); |
| 32 | +``` |
| 33 | + |
| 34 | +```java |
| 35 | +//Alias for calling executeParallel |
| 36 | +TaskExecutor.execute(new ExampleTask()); |
| 37 | +``` |
| 38 | + |
| 39 | +Using the TaskExecutor directly (like in the above example) means that you won't get any feedback to the UI. |
| 40 | + |
| 41 | +The preferred way to use a the `Task` class is by using the `TaskHandler.getActivityTaskHandler(FragmentManger)` method which returns a TaskHandler. The TaskHandler which is returned is loosely coupled to the Activity life-cycle using a internal Fragment which is retained across configuration changes. The `TaskHandler` makes sure you can retain your tasks across configuration changes. |
| 42 | + |
| 43 | +```java |
| 44 | +public class Main extends Activity implements Task.Callback, View.OnClickListener { |
| 45 | + private static final String TASK_DEMO = "demo-task"; |
| 46 | + |
| 47 | + //... onCreate etc. |
| 48 | + |
| 49 | + public TaskHandler getTaskHandler(){ |
| 50 | + return TaskHandler.getActivityTaskHandler(getSupportFragmentManager()); |
| 51 | + } |
| 52 | + |
| 53 | + protected void onStart() { |
| 54 | + super.onStart(); |
| 55 | + //Attach this activity as listener for the Task identified by tag TASK_DEMO |
| 56 | + getTaskHandler().attachListener(TASK_DEMO, this); |
| 57 | + } |
| 58 | + |
| 59 | + public void onClick(View v) { |
| 60 | + //Create a new task and execute it through the TaskHandler, |
| 61 | + //making this activity instance the tasks listener. |
| 62 | + ExampleTask task = new ExampleTask(TASK_DEMO); |
| 63 | + getTaskHandler().execute(task, this); |
| 64 | + } |
| 65 | + |
| 66 | + public void onPreExecute(Task<?, ?> task) { |
| 67 | + //Task started |
| 68 | + } |
| 69 | + |
| 70 | + public void onPostExecute(Task<?, ?> task) { |
| 71 | + //Task finished |
| 72 | + //This method will be called even after a rotation change, but within the new Activity instance) |
| 73 | + Toast.makeText(this, "Task finished", Toast.LENGTH_SHORT).show(); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
0 commit comments