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.

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

3 Answers3

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.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • Thanks for answer. Can you explain me a little more, please. What do you mean when you telling about "AA" subclass ? – ludriv Sep 02 '13 at 22:24
  • 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 ? – ludriv Sep 03 '13 at 12:49
  • 3
    Copy the generated code for the view creation from TemplateActivity_.onCreate() and move it to TemplateActivity.onCreate(). Delete the layout info from the annotation – koljaTM Sep 03 '13 at 13:27
2

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

Ginger McMurray
  • 1,275
  • 2
  • 20
  • 42
0

I think you should make you TempleteActivity an abstract class.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116