Android 在ButterKnife中解除绑定视图
示例
片段的视图生命周期与活动不同。在onCreateView中绑定片段时,在onDestroyView中将视图设置为null。当您调用bind为您执行此操作时,ButterKnife会返回Unbinder实例。在适当的生命周期回调中调用其unbind方法。
一个例子:
public class MyFragment extends Fragment {
@BindView(R.id.textView) TextView textView;
@BindView(R.id.button) Button button;
private Unbinder unbinder;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
unbinder = ButterKnife.bind(this, view);
//TODO使用字段...
return view;
}
@Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}注意:调用不是必需的,但建议您调用unbind(),onDestroyView()因为如果您的应用程序具有较大的堆栈,它将节省大量内存。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短