5

I have already read Some issues on github, but still haven't found solution.

My problem is that I cannot bind views in a child activity inherited from BaseActivty

Code in my BaseActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewGroup rootView = (ViewGroup) this.findViewById(android.R.id.content);
        View rootBaseView = getLayoutInflater().inflate(R.layout.activity_base, rootView, false);
        ButterKnife.bind(this, rootBaseView);
        ....
         int childLayoutID = getLayoutResId();
        if (childLayoutID != LayoutConstants.NULL_LAYOUT_ID) {
           View childRootView = getLayoutInflater().inflate(childLayoutID, mLinearLayoutFrameContainer, false);
           mLinearLayoutFrameContainer.addView(childRootView);
       }
        ....
        setContentView(rootBaseView);

So, I have abstract method

   public abstract
    @LayoutRes
    int getLayoutResId();

In any child activity I return specific layout resource.

All views in BaseActivity are injected correctly, but in any child activity views cannot be injected even with @Nullable annotation, view is null in runtime.

How can I solve this problem. maybe there are some workarounds ?

ADDITIONAL INFO

I have tried different ways like

    ButterKnife.bind(this);
    ButterKnife.bind(this,mLinearLayoutFrameContainer);

In child activity in onCreate method.

But still the same

But with fragment I have the same situation and it works.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.bind(this, view);
    Log.e("FRAGMENT", "ON VIEW CREATED " + getClass().getCanonicalName());

And in any child fragment I don't call ButterKnife inject methods at all, fields just annotated and of course I start using views only in onViewCreated after super call.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setupViews();
CROSP
  • 4,499
  • 4
  • 38
  • 89
  • 1
    I think Jake explained the issue in his comment on your bug report: you cannot inject into the same object twice - this is why it works on an additional Fragment bcz its another object and not on the same Activity. – and_dev Mar 10 '16 at 07:47
  • 1
    With baseactivity concept you can not implement it. you can check this - https://github.com/JakeWharton/butterknife/issues/198 & https://github.com/JakeWharton/butterknife/issues/434 – Wasim K. Memon Mar 10 '16 at 07:59
  • 1
    Thanks for the help. Can someone please explain on low level, why this is not possible, I think this will be informative and useful for a lot of developers and post is an answer for this question. (unfortunately I have not enough time to dive deep into implementation) – CROSP Mar 10 '16 at 08:30
  • I'm facing the same issue. Any chance it was resolved? – user2695433 Aug 03 '17 at 19:32

0 Answers0