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