Skip to content

Enhance custom classes

Pierre-Yves Ricau edited this page Feb 2, 2012 · 25 revisions

Since AndroidAnnotations 2.4

Enhancing custom classes

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 @EBean:

@EBean
public class MyClass {

}

Injecting enhanced classes

To use this enhanced class in another enhanced class or in an enhanced Android component, use @Bean:

@EBean
public class MyOtherClass {

  @Bean
  MyClass myClass;

}

Notice how you can chain dependencies:

@EActivity
public class MyActivity extends Activity {

  @Bean
  MyOtherClass myOtherClass;

}

Please note that there is currently no notion of scope (singleton, prototype, context scope).

This means that you always get a new instance when you use @Bean on a field.

Supported annotations

You can use most AA annotations in an @EBean class:

@EBean
public class MyClass {

  @SystemService
  NotificationManager notificationManager;

  @UiThread
  void updateUI() {

  }

}

View related annotations

You can use view related annotations ([@View](Injecting Views), @Click...) in your @EBean class:

@EBean
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.

Injecting the root context

You can inject the root Android component that depends on your @EBean class, using the @RootContext annotation. Please notice that it only gets injected if the context has the right type.

@EBean
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 {

  @Bean
  MyClass myClass;

}

Executing code after dependency injection

When the constructor of your @EBean annotated class is called, it's fields have not been injected yet. If you need to execute code at build time, after dependency injection, you should use the @AfterInject annotation on some methods.

@EBean
public class MyClass {

  @SystemService
  NotificationManager notificationManager;

  @Bean
  MyOtherClass dependency;

  public MyClass() {
    // notificationManager and dependency are null
  }

  @AfterInject
  public void doSomethingAfterInjection() {
    // notificationManager and dependency are set
  }

}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally