I'm am working on a project and am running into a bug that I can not seem to figure why it is happening. The problem is that I want to save the value of score when I flip the orientation of my phone. when I log the value of score I notice the problem, which is that the onCreateView
seems to be called twice when I change my phone's orientation. In the log during the first call it seems to save the value of score, but than it gets called again clearing the value of score.
- Why is the
onCreateView
getting called twice and reseting the value of score? How do I fix it so that
onCreateView
is called only once?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); }
}