Android 片段中的属性在最小化时变为null,并以“返回”;Don';“保持活动”;在…上
我有一个片段,其中我使用一个按钮,并在onCreateView()中调用该按钮 我在片段中声明这些属性:Android 片段中的属性在最小化时变为null,并以“返回”;Don';“保持活动”;在…上,android,android-fragments,android-lifecycle,fragment-lifecycle,Android,Android Fragments,Android Lifecycle,Fragment Lifecycle,我有一个片段,其中我使用一个按钮,并在onCreateView()中调用该按钮 我在片段中声明这些属性: Button button; Carrier carrier; 在onCreateView()方法中,我为它们赋值: button =v.findViewById(R.id.button); carrier = getCarrier(); 我在onStart()方法中使用了以下内容: 当我最小化并返回buttonValue时,它不会变为null,但载体值变为null 为什么会这样?这两个
Button button;
Carrier carrier;
在onCreateView()方法中,我为它们赋值:
button =v.findViewById(R.id.button);
carrier = getCarrier();
我在onStart()方法中使用了以下内容:
当我最小化并返回buttonValue时,它不会变为null,但载体值变为null
为什么会这样?这两个对象是否应该是一致的
承运人类别:
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);
}
}
我正在加载的这个片段是基于父片段的
这就像公共类ThisFragment扩展了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;
}
}
getCarrier()
实际上做了什么?getCarrier()使用片段的getInstance方法保存片段的颜色,并在加载片段时使用。还包括body,因为这似乎是问题的根源,所以您的意思是,当您返回按钮的onclicklistener时,该按钮的onclicklistener仍然有效,但该按钮的背景颜色不正确?按钮值存在时,保存该颜色的对象将变为空值。
public class ParentFragment() extends Fragment{
private Carrier carrier;
public void setCarrier(int color) {
carrier = new Carrier(new Integer(color));
}
public Carrier getCarrier() {
return carrier;
}
}