ButterKnife: Null views after Fragment recreation on a ViewPager
Asked Answered
P

0

12

I'm working with the latest version of Butterknife (8.2.1) and I have a problem: I'm using a ViewPager with 3 Fragments, when the Fragment is recreated all views become null!! The first 2 Fragments, no problem, but when I swipe to the 3rd and go back to the first one every view becomes null. What could be the problem?

I've added the ButterKnife.setDebug(true); to get more information about the problem and I'm getting this:

D/ButterKnife: Looking up view binder for com.example.overview.MeetupOverviewFragment
D/ButterKnife: HIT: Cached in view binder map.
D/ButterKnife: Looking up view binder for com.example.attendees.MeetupAttendeesFragment
D/ButterKnife: HIT: Cached in view binder map.
D/ButterKnife: Looking up view binder for com.example.attendees.ControllerListAttendees$ItemViewHolder
D/ButterKnife: HIT: Cached in view binder map.
D/ButterKnife: Looking up view binder for com.example.attendees.ControllerListAttendees$ItemViewHolder
D/ButterKnife: HIT: Cached in view binder map.
D/ButterKnife: Looking up view binder for com.example.attendees.ControllerListAttendees$ItemViewHolder
D/ButterKnife: HIT: Cached in view binder map.
I/InjectionManager: dispatchCreateOptionsMenu :com.example.MeetupDetailActivity
I/InjectionManager: dispatchPrepareOptionsMenu :com.example.MeetupDetailActivity

For the Fragments (as the wiki says) I'm doing this:

public class MeetupDocumentsFragment extends Fragment { 

    @BindView(R.id.list) CustomRecyclerView recyclerView;  

    private Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        View v = inflater.inflate(R.layout.my_layout, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}

For the RecyclerViewAdapters I'm using this code:

public class ControllerListAttendees extends RecyclerView.Adapter<ControllerListAttendees.ItemViewHolder>

    public static class ItemViewHolder extends RecyclerView.ViewHolder  {
        @BindView(R.id.some_imageview) ImageView imageAvatar;
        @BindView(R.id.some_label1) TextView labelName;
        @BindView(R.id.some_label2) TextView labelSurname;
        @BindView(R.id.some_label3) TextView labelAge;


        public ItemViewHolder(View v) {
            super(v);
            ButterKnife.bind(this, v);
        }
    }

}

In the meantime I could just set the OffscreenPageLimit of the ViewPager to 2 in order to avoid recreation but... I would rather do it the correct way :)

Can anyone help me with this?

Pika answered 18/7, 2016 at 13:16 Comment(3)
So where do you actually bind your views in your fragments?Pasty
I bind all the views in the onCreateView(). Should I move the ButterKnife.bind(this, v); call elsewhere?Pika
I have the same problem.Zither

© 2022 - 2024 — McMap. All rights reserved.