Inheritance activity with AndroidAnnotations
Asked Answered
P

3

6

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.

Puffball answered 1/9, 2013 at 22:41 Comment(4)
First of all, don't use System.out.println("postInit");. Use Log.d("DEBUG", "postInit"); instead. Do you now see the output in Logcat? Second, is setMainView(int) a method you created? Can you post its code here?Whereon
Right for Log. Yes I've created setMainView, I've added it in the question.Puffball
I am not familiar with AndroidAnnotations. But, shouldn't postInit() in ChildActivity be annotated with @AfterViews?Whereon
I tried too without any change. I will post a question directly on github.Puffball
I
7

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.

Infundibuliform answered 2/9, 2013 at 18:40 Comment(3)
Thanks for answer. Can you explain me a little more, please. What do you mean when you telling about "AA" subclass ?Puffball
You're right, not all annotations are passed on to subclasses. I think that @EActivity annotation replace the main content view of parent class (here TemplateActivity). And because of that, no layout is attached in content view. Are there another solution to templating view with AA ?Puffball
Copy the generated code for the view creation from TemplateActivity_.onCreate() and move it to TemplateActivity.onCreate(). Delete the layout info from the annotationInfundibuliform
S
2

Use @EActivity(R.layout.activity_child_one) in the child class and make the parent class abstract. That is working for me.

Saddletree answered 2/9, 2013 at 14:46 Comment(0)
S
0

I think you should make you TempleteActivity an abstract class.

Secor answered 1/9, 2013 at 22:45 Comment(1)
Thank you for answering, I've tried but it doesn't change no more.Puffball

© 2022 - 2024 — McMap. All rights reserved.