Skip to content
pyricau edited this page Jun 14, 2012 · 12 revisions

Since AndroidAnnotations 1.0

Get rid of AsyncTasks!!

@Background

The @Background annotation indicates that a method will run in a thread other than the ui thread.

Usage example:

void myMethod() {
    someBackgroundWork("hello", 42);
}

@Background
void someBackgroundWork(String aParam, long anotherParam) {
    [...]
}

The method is executed on a separate thread, but this doesn't necessarily mean that a new thread will be started, because we use a shared cached thread pool executor (which can be replaced) to prevent creating too much threads.

This also means that two @Background methods may run in parallel

@UiThread

The @UiThread annotation indicates that a method will run in the ui thread.

Usage example:

void myMethod() {
    doInUiThread("hello", 42);
}

@UiThread
void doInUiThread(String aParam, long anotherParam) {
    [...]
}

No more AsyncTask<Param, Progress, Result>!!

Delay

If you need to add a delay before a method is run on the UI Thread, you can use the delay parameter:

@UiThread(delay=2000)
void doInUiThreadAfterTwoSeconds() {
}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally