@Retention(value=CLASS) @Target(value=METHOD) public @interface UiThread
Should be used on method that must be run in the Ui thread
The annotated method MUST return void and MAY contain parameters.
The generated code is based on a local Handler instance.
Sometimes you may want to delay execution of a Ui thread method. To do so,
you should use the delay() field.
Example :
@EBean
public class MyBean {
@UiThread(delay = 2000)
void uiThreadTask() {
// ...
}
}
Prior to 3.0, UiThread annotated method calls was always added in the
handler execution queue to ensure that execution was done in Ui thread. In
3.0, we kept the same behavior for compatibility purpose.
But, if you want to optimize UiThread calls, you may want to change
propagation() value to REUSE. In this configuration,
the code will make a direct call to the method if current thread is already
Ui thread. If not, we're falling back to handler call.
Example :
@EBean
public class MyBean {
@UiThread
void uiThreadTask() {
// This code is executed via the handler
// The following method will be directly executed instead of using
// handler
uiThreadTaskReused();
}
@UiThread(propagation = REUSE)
void uiThreadTaskReused() {
// ...
}
}
You can cancel UiThread tasks if you provide an id (which cannot be an empty
string) with the id() parameter. Please note tasks which use
REUSE propagation() cannot have an id hence cannot be
cancelled. To cancel all UiThread tasks with a given id, call
UiThreadExecutor#cancelAll(String).
Example :
@EBean
public class MyBean {
@UiThread(id = "myId")
void uiThreadTask() {
// do sg
}
}
...
UiThreadExecutor.cancelAll("myId");
Background,
Handler,
UiThreadExecutor.cancelAll(String)| Modifier and Type | Optional Element and Description |
|---|---|
long |
delay
The delay of the execution in milliseconds.
|
String |
id
Identifier for cancellation.
|
UiThread.Propagation |
propagation
If propagation is
UiThread.Propagation.REUSE, the method will check first
if it is inside the UI thread already. |
public abstract long delay
public abstract UiThread.Propagation propagation
UiThread.Propagation.REUSE, the method will check first
if it is inside the UI thread already. If so, it will directly call the
method instead of using the handler. The default value is
UiThread.Propagation.ENQUEUE, which will always call the handler.
When using a non-zero delay the propagation parameter is ignored.UiThread.Propagation.ENQUEUE to always call the handler,
UiThread.Propagation.REUSE, to check whether it is already on the
UI threadpublic abstract String id
UiThreadExecutor.cancelAll("my_background_id");
Copyright © 2010–2018 simpligility technologies inc.. All rights reserved.