0

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.

Luís Henriques
  • 604
  • 1
  • 10
  • 30

1 Answers1

1

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?

firegloves
  • 5,581
  • 2
  • 29
  • 50
  • Thanks for the answer. Sorry, I can't really post much of the code here. But no, I don't have onCreateView() in NavMenuStats. It is using onCreateView() from its parent class. I tried overriding it and calling the super method / leaving it empty, but no joy. Is there any other way of ignoring the onCreateView from the parent fragment? – Luís Henriques May 04 '18 at 15:53
  • can you remove it also from your parent fragment or you need it at all costs? I mean, you could annotate also parent fragment and let AndroidAnnotations work for you also there. Could it be a possible solution? – firegloves May 04 '18 at 15:55
  • That was my first solution :D But sadly no, I can't remove it. – Luís Henriques May 04 '18 at 15:58
  • Wow. Thank you very much. Let me know if you need more info. – Luís Henriques May 04 '18 at 16:29
  • I ve already answered :) let me know if you can detect your problem – firegloves May 04 '18 at 16:30
  • Great! It worked perfectly. I simply forgot to add the child with the "_" at the end on the adapter. I can't believe I overlooked it, but I'd never get there without your answer. Thank you. – Luís Henriques May 04 '18 at 16:55