-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance custom classes
Since AndroidAnnotations 2.3
You can use annotations in a class that is not a standard Android component (such as an Activity, a Service).
You just need to annotate it with @Enhanced:
@Enhanced
public class MyClass {
}To use this enhanced class in another enhanced class or in an enhanced Android component, use @Inject:
@Enhanced
public class MyOtherClass {
@Inject
MyClass myClass;
}Notice how you can chain dependencies:
@EActivity
public class MyActivity extends Activity {
@Inject
MyOtherClass myOtherClass;
}You can use most AA annotations in an @Enhanced class:
@Enhanced
public class MyClass {
@SystemService
NotificationManager notificationManager;
@UiThread
void updateUI() {
}
}You can use view related annotations ([@View](Injecting Views), @Click...) in your @Enhanced class:
@Enhanced
public class MyClass {
@ViewById
TextView myTextView;
@Click(R.id.myButton)
void handleButtonClick() {
}
}Notice that this will only work if the root Android component that depends on MyClass is an activity with a layout that contains the needed views. Otherwise, myTextView will be null, and handleButtonClick() will never be called.
You can inject the root Android component that depends on your @Enhanced class, using the @RootContext annotation. Please notice that it only gets injected if the context has the right type.
@Enhanced
public class MyClass {
@RootContext
Context context;
// Only injected if the root context is an activity
@RootContext
Activity activity;
// Only injected if the root context is a service
@RootContext
Service service;
// Only injected if the root context is an instance of MyActivity
@RootContext
MyActivity myActivity;
}In the MyClass instance referenced by the following activity, the service field of MyClass (see above) will be null.
@EActivity
public class MyActivity extends Activity {
@Inject
MyClass myClass;
}14/06/2012 The 2.6 release is out
- Get started!
- Cookbook, full of recipes
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow