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.

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? Commented Sep 1, 2013 at 22:54
  • Right for Log. Yes I've created setMainView, I've added it in the question. Commented Sep 1, 2013 at 22:58
  • I am not familiar with AndroidAnnotations. But, shouldn't postInit() in ChildActivity be annotated with @AfterViews? Commented Sep 1, 2013 at 23:19
  • I tried too without any change. I will post a question directly on github. Commented Sep 2, 2013 at 6:25

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer. Can you explain me a little more, please. What do you mean when you telling about "AA" subclass ?
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 ?
Copy the generated code for the view creation from TemplateActivity_.onCreate() and move it to TemplateActivity.onCreate(). Delete the layout info from the annotation
2

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

Comments

0

I think you should make you TempleteActivity an abstract class.

1 Comment

Thank you for answering, I've tried but it doesn't change no more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.