Search code examples
javaandroiddependency-injectionandroid-annotations

How to use AndroidAnnotations on simple java class


I have a simple Java class like following, which doesn't extend from anything.

public class WeatherPresenter {

    @App
    CrossWeatherApplication application;

    @Background
    public void loadWeather(String city) {

    }

}

So, up there, how can i use AndroidAnnotations like @App and @Background on that class ?


Solution

  • In AndroidAnnotations, all classes when you use annotations, have to be marked with the corresponding "enhanced component" annotation. In your example, you want to use @EBean.

    @EBean
    public class WeatherPresenter {
    
        @App
        CrossWeatherApplication application;
    
        @Background
        public void loadWeather(String city) {
    
        }
    
    }
    

    Then you can inject an instance of this bean into your Activity for example:

    @EActivity
    public class YourActivity extends Activity {
    
        @Bean
        WeatherPresenter weatherPresenter;
    
        @AfterInject
        void afterInject() {
            weatherPresenter.loadWeather("New York");
        }
    }