Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

After injection

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

Since AndroidAnnotations 2.4

Executing code after dependency injection

If you need to execute code at build time, after dependency injection, you should use the @AfterInject annotation on some methods.

@Enhanced
public class MyClass {

  @SystemService
  NotificationManager notificationManager;

  @Inject
  MyOtherClass dependency;

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

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

}

Or

@EActivity
public class MyActivity extends Activity {

  @SystemService
  NotificationManager notificationManager;

  @Inject
  MyOtherClass dependency;

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

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

}

Please note that the view related injection hasn't occurred yet when @AfterInject methods are executed. You should use @AfterViews for such needs, see [Injecting Views](Injecting Views).

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally