Inheritance with AndroidAnnotations

I'm having trouble working with AndroidAnnotations. I have a parent activity named TemplateActivity:

@EActivity(R.layout.activity_template) @NoTitle public class TemplateActivity extends Activity { // some views // ... @ViewById(R.id.main_framelayout) FrameLayout mainFrameLayout; @AfterViews public void postInit() { Log.d("DEBUG", "postInit"); // never called, strange... } public void setMainView(int layoutResID) { mainFrameLayout.addView(LayoutInflater.from(this).inflate(layoutResID, null)); } } 

And in my second Office, I want to populate mainFrameLayout using an XML pointer layout as follows:

 @EActivity public class ChildActivity extends TemplateActivity { @Override public void postInit() { super.postInit(); setMainView(R.layout.activity_child_one); } } 

When I want to start an action, my ChildActivity is empty and postInit was never called. Can someone tell me what is wrong? Thanks for the promotion.

+6
source share
3 answers

Annotations in the parent class will create the TemplateActivity_ class with the specified layout. The child class inherits the "normal" material from this parent class, but has its own subclass AA (ChildActivity_). Therefore, you must specify the layout in order to use it. Just take a look at the created classes to see what happens there.

AA works by creating a new subclass for your annotated classes (e.g. TemplateActivity_ extends TemplateActivity) that contains the code needed to achieve the results of your annotations. For example, in this class, the onCreate () method will instantiate the desired layout, methods annotated with @Background are overridden by another implementation that calls the original method in the background thread. AndroidAnnotations doesn't actually do anything at run time, everything can be seen in the classes it creates, just look in the .apt_generated folder (or wherever you generate the classes). It can also be useful if he doesn’t quite do what you want, because then you can just look at what he is doing and do it yourself, as you need it.

In your case, the inheritance hierarchy looks like this:

 TemplateActivity (with annotations) L--> TemplateActivity_ (with generated code for the layout) L--> ChildActivity (your other class, no generated code) L--> ChildActivity_ (with code generated for the annotations in ChildActivity) 

Afaik not all annotations are passed to subclasses.

+6
source

Use @EActivity (R.layout.activity_child_one) in the child class and create an abstract parent class. This works for me.

+2
source

I think you should make you TempleteActivity an abstract class.

0
source

Source: https://habr.com/ru/post/952940/


All Articles