A Fragment base class, which is inherited by two Fragment respectively. When switching to the second Fragment, the control for the layout cannot be found. It should be unbound. How should I change this situation?
here are three classes and screenshots of error reports
public abstract class LazyFragment extends Fragment {
private Unbinder mUnbinder;
private boolean isLoaded; //,,
private View mContainerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//
if (getUserVisibleHint() && !isLoaded) {
mContainerView = inflater.inflate(contentViewId(), container, false);
isLoaded = true;
} else {
//
mContainerView = inflater.inflate(R.layout.lazy_loading, container, false);
mContainerView.setBackgroundColor(lazyFragmentBackground());
}
return mContainerView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mUnbinder = ButterKnife.bind(this, view);
initView(savedInstanceState, view);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && !isLoaded) {
if (mContainerView instanceof ViewGroup) { //viewgroupview
ViewGroup group = ((ViewGroup) mContainerView);
group.removeAllViews();
View view = LayoutInflater.from(getActivity()).inflate(contentViewId(), group, false);
group.addView(view);
super.onViewCreated(mContainerView, null);
lazyLoad();
isLoaded = true;
}
}
}
protected int lazyFragmentBackground() {
return ContextCompat.getColor(getActivity(), R.color.lazy_fragment_background);
}
protected abstract void lazyLoad();
public abstract void initView(Bundle savedInstanceState, View view);
public abstract int contentViewId();
@Override
public void onDestroy() {
super.onDestroy();
if (mUnbinder != null) {
mUnbinder.unbind();
mUnbinder = null;
}
}
}
public class OneFragment extends LazyFragment {
@BindView(R.id.tv_one)
TextView mTextView;
private static final String TAG = "OneFragment";
@Override
public int contentViewId() {
Log.d(TAG, "5678contentViewId: 11111111111111");
return R.layout.fragment_one;
}
@Override
public void initView(Bundle savedInstanceState, View view) {
Log.d(TAG, "5678initView: 11111111111111");
mTextView.setText("aaaaaaaaaaaaa");
}
@Override
protected void lazyLoad() {
Log.d(TAG, "5678setUserVisibleHint: 111111111111");
}
}
public class TwoFragment extends LazyFragment {
@BindView(R.id.tv_two)
TextView mTextView;
private static final String TAG = "TwoFragment";
@Override
public int contentViewId() {
Log.d(TAG, "5678contentViewId: 22222222222222222");
return R.layout.fragment_two;
}
@Override
public void initView(Bundle savedInstanceState, View view) {
Log.d(TAG, "5678initView: 2222222222222222");
mTextView.setText("bbbbbbbbbbbbbb");
}
@Override
protected void lazyLoad() {
Log.d(TAG, "5678setUserVisibleHint: 2222222222222");
}
}