Android-Annotations and inheritance
Asked Answered
S

1

8

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?

Soares answered 27/2, 2013 at 17:30 Comment(1)
C
11

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)

Cannice answered 27/2, 2013 at 18:32 Comment(2)
method afterTestBaseFragmentViews should not be private. In this case AA can't access this methodGesso
what if TestBaseFragment has annotations like ViewById ? In my case, these annotations are not seen in the generated class TestFragment_. The same for annotation Click.Cistern

© 2022 - 2024 — McMap. All rights reserved.