저는 프로젝트에서 일하고 있는데 왜 그런 일이 일어나고 있는지 알 수없는 버그가 있습니다. 문제는 휴대 전화의 방향을 바꿀 때 점수의 가치를 저장하려고한다는 것입니다. 내가 점수의 가치를 기록 할 때 나는 전화기의 방향을 바꿀 때 onCreateView
이 두 번 호출되는 것처럼 보이는 문제를 알아 차린다. 첫 번째 호출 중 로그에서 점수의 값을 저장하는 것으로 보이지만 다시 호출되어 점수의 값을 지우는 것보다.android fragment savedInstanceState 값이 화면 회전시 재설정됩니다.
- 왜
onCreateView
이 두 번 호출되어 스코어 값을 다시 설정합니까? onCreateView
을 한 번만 호출하도록 수정하려면 어떻게해야합니까? (때문에, 당신의 조각의onCreate
방법if (savedInstanceState != null) { score = savedInstanceState.getInt("score"); }
이라고 당신이 당신의 장치
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); }
} 당신의 조각 코드에서
안녕하세요, http://stackoverflow.com/a/10996647/5241603을 읽어주세요. 도움이 될 수 있습니다. –
나는 그것을 보았고, 지금 나는 그것이 일어나는 이유를 이해하지만, 나는 그것을 고치는 방법에 대한 그의 설명을 이해하지 못한다. – topgun741