Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance activity with AndroidAnnotations

I have some trouble to build activities 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 Activity, I want to fill mainFrameLayout with anoter layout xml like that :

@EActivity
public class ChildActivity extends TemplateActivity
{
    @Override
    public void postInit()
    {
        super.postInit();

        setMainView(R.layout.activity_child_one);       
    }
}

When I want to startActivity, my ChildActivity is blank and postInit was never called. Can anybody tell me what is wrong? Thanks for advance.

like image 925
ludriv Avatar asked Sep 01 '13 22:09

ludriv


1 Answers

The annotation in your parent class will result in a class TemplateActivity_ with the specified layout. The child class will inherit the "normal" stuff from that parent class, but have its own AA subclass (ChildActivity_). So you should specify the layout to use there as well. Just take a look at the generated classes to see what is going on there.

AA works by generating a new subclass for your annotated classes (e.g. TemplateActivity_ extends TemplateActivity) that contains the code necessary to achieve the results of your annotations. For example, in this class the onCreate() method will instantiate the layout needed, methods annotated with @Background get overridden with another implementation that calls the original method in a background thread. AndroidAnnotations doesn't really do anything at runtime, everything can be seen in the classes it generates, just look into the .apt_generated folder (or wherever you generated the classes to). This can also be helpful if it doesn't quite do what you want, because you can then just take a look at what it does and do it yourself in the way you need it.

In your case, the inheritance hierarchy is 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 on to subclasses.

like image 160
koljaTM Avatar answered Sep 30 '22 19:09

koljaTM