Enhancing the Application class

Csaba Kozák edited this page Mar 16, 2016 · 11 revisions

Since AndroidAnnotations 2.4

You can enhance your Android Application class with the @EApplication annotation:

@EApplication
public class MyApplication extends Application {

}

You can then start using most AA annotations, except the ones related to views and extras:

@EApplication
public class MyApplication extends Application {

  public void onCreate() {
    super.onCreate();
    initSomeStuff();
  }

  @SystemService
  NotificationManager notificationManager;

  @Bean
  MyEnhancedDatastore datastore;

  @RestService
  MyService myService;
  
  @OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
  UserDao userDao;

  @Background
  void initSomeStuff() {
    // init some stuff in background
  }
}

Injecting your application class

Since AndroidAnnotations 2.1

You can inject the application class using the @App annotation:

@EActivity
public class MyActivity extends Activity {

  @App
  MyApplication application;

}

It also works for any kind of annotated component, such as @EBean:

@EBean
public class MyBean {

  @App
  MyApplication application;

}

Since AndroidAnnotations 3.0, the application class must be annotated with @EApplication.

Method based injection

Since AndroidAnnotations 4.0.0

@EBean
public class MyBean {

  @App
  void setApplication(MyApplication application){
    // do something with application
  }

}

14/10/2018 The 4.5.1 release is out !

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.