我正在研究一个项目,并且遇到了一个我无法理解为什么会发生这种情况的错误。问题是,当我翻转手机的方向时,我想保存得分值。当我记录得分的值时,我注意到问题,即当我改变手机的方向时onCreateView
似乎被调用了两次。在第一次调用期间的日志中,它似乎保存了分数值,但是再次调用它来清除分数值。
onCreateView
被调用两次并重置得分值?如何修复它以便只调用onCreateView
一次?
public class SteeringFragment extends Fragment {
Drawable transparentRadioButton;
RadioButton rbExcellent, rbGood, rbFair, rbPoor, rbFailing;
LinearLayout llExcellent, llGood, llFair, llPoor, llFailing;
int score;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.steering_title);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_steering, container, false);
if (savedInstanceState != null){
score = savedInstanceState.getInt("score");
}
Log.d("test", String.valueOf(score));
llExcellent = (LinearLayout)view.findViewById(R.id.steeringLLExcellent);
llGood = (LinearLayout)view.findViewById(R.id.steeringLLGood);
llFair = (LinearLayout)view.findViewById(R.id.steeringLLFair);
llPoor = (LinearLayout)view.findViewById(R.id.steeringLLPoor);
llFailing = (LinearLayout)view.findViewById(R.id.steeringLLFailing);
rbExcellent = (RadioButton)view.findViewById(R.id.steeringRadioExcellent);
rbGood = (RadioButton)view.findViewById(R.id.steeringRadioGood);
rbFair = (RadioButton)view.findViewById(R.id.steeringRadioFair);
rbPoor = (RadioButton)view.findViewById(R.id.steeringRadioPoor);
rbFailing = (RadioButton)view.findViewById(R.id.steeringRadioFailing);
//used to hide the radio button
transparentRadioButton = new ColorDrawable(Color.TRANSPARENT);
rbExcellent.setButtonDrawable(transparentRadioButton);
rbGood.setButtonDrawable(transparentRadioButton);
rbFair.setButtonDrawable(transparentRadioButton);
rbPoor.setButtonDrawable(transparentRadioButton);
rbFailing.setButtonDrawable(transparentRadioButton);
//if score already has value set it to the value
if (score != 0){
switch (score){
case 5:
rbExcellent.setChecked(true);
rbGood.setChecked(false);
rbFair.setChecked(false);
rbPoor.setChecked(false);
rbFailing.setChecked(false);
rbExcellent.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.excellentColor));
llExcellent.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
break;
case 4:
rbExcellent.setChecked(false);
rbGood.setChecked(true);
rbFair.setChecked(false);
rbPoor.setChecked(false);
rbFailing.setChecked(false);
rbGood.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.goodColor));
llGood.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
break;
case 3:
rbExcellent.setChecked(false);
rbGood.setChecked(false);
rbFair.setChecked(true);
rbPoor.setChecked(false);
rbFailing.setChecked(false);
rbFair.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.fairColor));
llFair.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
break;
case 2:
rbExcellent.setChecked(false);
rbGood.setChecked(false);
rbFair.setChecked(false);
rbPoor.setChecked(true);
rbFailing.setChecked(false);
rbPoor.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.poorColor));
llPoor.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
break;
case 1:
rbExcellent.setChecked(false);
rbGood.setChecked(false);
rbFair.setChecked(false);
rbPoor.setChecked(false);
rbFailing.setChecked(true);
rbFailing.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.failingColor));
llFailing.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
break;
}
}
rbExcellent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rbExcellent.setChecked(true);
rbGood.setChecked(false);
rbFair.setChecked(false);
rbPoor.setChecked(false);
rbFailing.setChecked(false);
}
});
rbGood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rbExcellent.setChecked(false);
rbGood.setChecked(true);
rbFair.setChecked(false);
rbPoor.setChecked(false);
rbFailing.setChecked(false);
}
});
rbFair.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rbExcellent.setChecked(false);
rbGood.setChecked(false);
rbFair.setChecked(true);
rbPoor.setChecked(false);
rbFailing.setChecked(false);
}
});
rbPoor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rbExcellent.setChecked(false);
rbGood.setChecked(false);
rbFair.setChecked(false);
rbPoor.setChecked(true);
rbFailing.setChecked(false);
}
});
rbFailing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rbExcellent.setChecked(false);
rbGood.setChecked(false);
rbFair.setChecked(false);
rbPoor.setChecked(false);
rbFailing.setChecked(true);
}
});
rbExcellent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
rbExcellent.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.excellentColor));
llExcellent.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
score = 5;
}else {
rbExcellent.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.textColor));
llExcellent.setBackground(null);
llExcellent.setPadding(0,0,0,0);
}
}
});
rbGood.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
rbGood.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.goodColor));
llGood.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
score = 4;
}else {
rbGood.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.textColor));
llGood.setBackground(null);
llGood.setPadding(0,0,0,0);
}
}
});
rbFair.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
rbFair.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.fairColor));
llFair.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
score = 3;
}else {
rbFair.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.textColor));
llFair.setBackground(null);
llFair.setPadding(0,0,0,0);
}
}
});
rbPoor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
rbPoor.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.poorColor));
llPoor.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
score = 2;
}else {
rbPoor.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.textColor));
llPoor.setBackground(null);
llPoor.setPadding(0,0,0,0);
}
}
});
rbFailing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
rbFailing.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.failingColor));
llFailing.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.radio_boxes_borders));
score = 1;
}else {
rbFailing.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.textColor));
llFailing.setBackground(null);
llFailing.setPadding(0,0,0,0);
}
}
});
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("score", score);
}
}
答案 0 :(得分:0)
在您的片段代码中尝试存储以下行:
->middleware('auth:api');
在您的片段的if (savedInstanceState != null) {
score = savedInstanceState.getInt("score");
}
方法中,每次调用设备onCreate
时都会调用(因为重新绘制了UI),并且会清除其中的一些参数。