我有一个片段,我在其中使用一个按钮并在 onCreateView() 中调用该按钮。
我在片段中声明了这些属性:
Button button;
Carrier carrier;
在 onCreateView() 方法中,我为它们赋值:
button =v.findViewById(R.id.button);
carrier = getCarrier();
我在 onStart() 方法中使用这些:
button.setOnClickListener(this);
button.setBackgroundColor(ContextCompat.getColor(getContext(),carrier.getColourResource()));
当我最小化并返回时,buttonValue 不会变为空,但载体值变为空。
为什么会这样?两个对象不应该是统一的吗?
承运人等级:
public class Carrier {
private Integer colourResource = 0;
public Carrier(Integer colourResource) {
setColourResource(colourResource);
}
public Integer getColourResource() {
return colourResource;
}
public void setColourResource(int resource) {
colourResource = new Integer(resource);
}
}
我正在加载的这个片段基于父片段
它像是public class ThisFragment extends ParentFragment {}
父片段:
public class ParentFragment() extends Fragment{
private Carrier carrier;
public void setCarrier(int color) {
carrier = new Carrier(new Integer(color));
}
public Carrier getCarrier() {
return carrier;
}
}