Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-Annotations and inheritance

i am having trouble with android-annotations and inheritance:

@EFragment(R.layout.fragment_foo)
public class TestBaseFragment {
    @AfterViews
    public void afterTestBaseFragmentViews() {
   }
}

@EFragment(R.layout.fragment_foo)
public class TestFragment extends TestBaseFragment {
    @AfterViews
    public void afterTestFragmentViews() {
   }
}

generates:

public final class TestFragment_
    extends TestFragment
{
    ...

    private void afterSetContentView_() {
        afterTestFragmentViews();
        afterTestBaseFragmentViews();
    }
    ...

how can I make sure that afterTestBaseFragmentViews() is called before afterTestFragmentViews() ? Or can you point me to any document which describes how to do inheritance with AndroidAnnotations?

like image 943
ligi Avatar asked Feb 27 '13 17:02

ligi


People also ask

Are annotations inherited?

Annotations, just like methods or fields, can be inherited between class hierarchies. If an annotation declaration is marked with @Inherited , then a class that extends another class with this annotation can inherit it.

Are Spring annotations inherited?

Annotations on methods are not inherited by default, so we need to handle this explicitly.

What is inheritance in Android?

Inheritance allows the class to use the states and behavior of another class using extends keyword. Inheritance is-a relationship between a Base class and its child class.

What are Android annotations?

Annotations allow you to provide hints to code inspections tools like Lint, to help detect these more subtle code problems. They are added as metadata tags that you attach to variables, parameters, and return values to inspect method return values, passed parameters, local variables, and fields.


1 Answers

It's just some sort of workaround

Using an abstract class:

@EFragment(R.layout.fragment_foo)
public abstract class TestBaseFragment {

    @AfterViews
    protected void afterTestBaseFragmentViews() {
        // do something
        afterView();
    }

    protected abstract void afterView();

}

@EFragment(R.layout.fragment_foo)
public class TestFragment extends TestBaseFragment {

    @Override
    protected void afterView() {
        // do something after BaseFragment did something
    }

}

Using simple subclassing:

@EFragment(R.layout.fragment_foo)
public class TestBaseFragment {

    @AfterViews
    protected void afterTestBaseFragmentViews() {
        afterView();
    }

    public void afterView() {
        // do something
    }

}

@EFragment(R.layout.fragment_foo)
public class TestFragment extends TestBaseFragment {

    @Override
    public void afterView() { 
        super.afterView();
        // do something after BaseFragment did something
    }

}

I hope this is what you were looking for. (Not tested - just written in notepad)

like image 132
Basic Coder Avatar answered Sep 29 '22 17:09

Basic Coder