Articles → Android → Save The State Of The Control If Orientation Is Changed In Android

Save The State Of The Control If Orientation Is Changed In Android






Issue In State After Changing Orientation




Picture showing the timepicker control with the current time
Click to Enlarge



Picture showing the timepicker control with time modified manually
Click to Enlarge



Picture showing changing the orientation of the screen
Click to Enlarge




How To Save State?






@Override
public void onSaveInstanceState(Bundle outState) {

  super.onSaveInstanceState(outState);

  TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);
  outState.putInt("hour", tp.getHour());
  outState.putInt("minute", tp.getMinute());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);
  tp.setHour(savedInstanceState.getInt("hour"));
  tp.setMinute(savedInstanceState.getInt("minute"));
}






Picture showing the timepicker control with the current time
Click to Enlarge



Picture showing the timepicker control with time modified manually
Click to Enlarge



Picture showing changing the orientation of the screen
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, January 9, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250