Search code examples
javaandroidandroid-fragmentsandroid-annotations

AndroidAnnotations not working with inherited class


I have a fragment (NavMenuStats) that extends a parent fragment (NavMenu).

This parent fragment overrides the onCreateView method, like so:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    if (some condition) {

        View view = ...

        // Do stuff and return view. Everything works well

        return view;

    } else {

        return null; // since @Nullable
    }
}

But when I use AndroidAnnotations on the class that inherits this fragment, for which "some condition" is false, it doesn't return the view. Everything is simply blank and @AfterViews doesn't even get called.

I also tried, instead of returning null, to:

...

} else {
    return super.onCreateView(inflater, container, savedInstanceState);
}

I know I have to let AndroidAnnotations to handle the layout inflation, as stated here: androidannotations @AfterViews is not calling and editText is null

That is why I'm returning null or the super method in the parent fragment's onCreateView().

What am I missing? Any ideas?

Thank you very much.


Solution

  • It's difficult to answer without your NavMenuStats code, but I think your problem resides there.

    I think you have added onCreateView method into your NavMenuStats too, so you are preventing AndroidAnnotations to write it for you.

    Try to remove completely onCreateView method by your @EFragment annotated class.

    With this situation it works:

    MainFragment

    public class MainFragment extends Fragment{
    
        public boolean nullView = false;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            return nullView ? null : inflater.inflate(R.layout.fragment_main, container, false);
        }
    }
    

    SonFragment

    @EFragment(R.layout.fragment_son)
    public class SonFragment extends MainFragment {
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.nullView = true;
        }
    }
    

    Maybe you forget to add generated son fragment with _ at the end?