Android TimePicker time disappears after changing orientation

I use TimePicker in API 1.5, and when the orientation has changed on my device (the G1 stock running on 1.6, although not on the 1.5 / 1.6 emulator), the hour field remains empty. He still remembers the hour, he just does not show it. Is there a workaround for this?

The same problem was described by someone else here:

http://groups.google.com/group/android-beginners/browse_thread/thread/b4288004021b876/de5899a2bb291ab5

There was nothing useful - could StackOverflow do better?

+7
android
source share
10 answers

Just move the call to the TimePicker.setCurrentHour and TimePicker.setCurrentMinute methods to the onResume method of your activity.

+6
source share

As already mentioned, overriding the onResume () method will sort this problem.

@Override public void onResume() { super.onResume(); TimePicker tmp = (TimePicker) this.findViewById(R.id.yourTimePicker); tmp.setCurrentHour(7); // This will also set on rotate } 
+3
source share

Take a look at this: http://code.google.com/p/android/issues/detail?id=22824 No matter what you do, you cannot solve the problem without destroying the Activity and re-creating it when the rotation occurs . To avoid introducing an infinite loop, try checking to see if there was a stored value of InstanceState! = Null in onCreate. Something like this will work:

 if (savedInstanceState!=null){ Intent myIntent = new Intent(getApplicationContext(), CurrentClassName.class); startActivityForResult(myIntent, 0); finish(); } 
+1
source share

In onSaveInstanceState() for your activity, get the hour value from TimePicker and put it in the Bundle . In onRestoreInstanceState() for your activity, get the hour out of the Bundle and put it in the TimePicker .

TimePicker should handle this on its own, but this might work around the problem for you.

0
source share

I lost a lot of sleep and realized this. When you use DialogPreference, you need to override the showDialog method and reset your currentHour and currentMinute. Like this:

 @Override protected void showDialog(Bundle state) { super.showDialog(state); mBeginShiftTp.setCurrentHour(mBeginShiftTp.getCurrentHour()); mBeginShiftTp.setCurrentMinute(mBeginShiftTp.getCurrentMinute()); mEndShiftTp.setCurrentHour(mEndShiftTp.getCurrentHour()); mEndShiftTp.setCurrentMinute(mEndShiftTp.getCurrentMinute()); 

}

0
source share

Based on the link provided by DanRoss, the problem is with the collector not updating the hour if the hour has not changed. As for orientation changes, this behavior is malfunctioning, because an hour before the turn will always be equal to an hour after the turn.

For those using TimePickerDialog , the way to this is to extend the default implementation and make it update the hour by updating the time (setting the hour and minute to 0 in my example) and then update the hour to the actual hour.

Perhaps you can build something similar using just the TimePicker control.

Here is a working example for TimePickerDialog:

 import android.content.Context; import android.os.Bundle; import android.widget.TimePicker; public class TimePickerDialog extends android.app.TimePickerDialog { private final String HOUR_KEY = "Hour"; private final String MINUTE_KEY = "Minute"; private int hourOfDay; private int minute; public TimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) { super(context, callBack, hourOfDay, minute, is24HourView); this.hourOfDay = hourOfDay; this.minute = minute; } @Override public void onTimeChanged (TimePicker view, int hourOfDay, int minute) { super.onTimeChanged(view, hourOfDay, minute); this.hourOfDay = hourOfDay; this.minute = minute; } @Override public Bundle onSaveInstanceState() { Bundle savedInstanceState = super.onSaveInstanceState(); if (savedInstanceState == null) { savedInstanceState = new Bundle(); } savedInstanceState.putInt(HOUR_KEY, this.hourOfDay); savedInstanceState.putInt(MINUTE_KEY, minute); return savedInstanceState; } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // The code for updating the hour is currently not doing anything // if the hour has not changed. // Force it to update by setting it to zero first this.updateTime(0, 0); this.hourOfDay = savedInstanceState.getInt(HOUR_KEY); this.minute = savedInstanceState.getInt(MINUTE_KEY); // Now it will update this.updateTime(this.hourOfDay, this.minute); } } 
0
source share

This error is related to the official Android forum Error 5483 . Well, and this discussion, they offer a simple solution, and it works for me. I added the android:onConfigurationChanges=orientation attribute to my activity on AndroidManifest.xml .

Important: depending on your activity (other widgets ...) it may have side effects

0
source share

I understand that I'm a little late for the party, but seeing that this problem still exists, I believe that it deserves a real answer.

From what I read elsewhere, the problem is with individual fields of the TimePicker number having exactly the same identifier. The reason this problem is because when you rotate the screen, android closes all your dialogs, opens all your dialogs again using the typical onCreateDialog β†’ onPrepareDialog, and then, most importantly, after you have done your dialogs, he returns and restores what was before. This means that no matter what you do in advance, the android will come in and break your TimePicker.

The answer, although not the most beautiful option, should appear after android and install it again.

Here is how I did it, I just made my own TimePicker:

 public class MyTimePicker extends TimePicker { public MyTimePicker(Context context) { super(context); } public MyTimePicker(Context context, AttributeSet attrs) { super(context, attrs); } public MyTimePicker(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private final static String B_MIN = "bMin"; private final static String B_HOUR = "bHour"; private final static String B_SUPER = "bSuper"; // here the first key, we're going to override how the TimePicker saves // its state @Override public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(B_SUPER, super.onSaveInstanceState()); bundle.putInt(B_MIN, getCurrentMinute()); bundle.putInt(B_HOUR, getCurrentHour()); return bundle; } // this function gets called when we are restoring, but before android // does its thing @Override public void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; super.onRestoreInstanceState(bundle.getParcelable(B_SUPER)); // here it is, not the prettiest option, but it gets the job done // we simply wait 0.1 sec and then update the TimePicker // since all of this happens in the same thread, its guarenteed to // be after the android reset // also, this handler gets the HandlerLeak warning, I don't // think this can leak though seeing as it only called once // with a 100 ms delay (new Handler() { @Override public void handleMessage(Message msg) { setCurrentMinute(bundle.getInt(B_MIN)); setCurrentHour(bundle.getInt(B_HOUR)); } }).sendEmptyMessageDelayed(0, 100); return; } super.onRestoreInstanceState(state); } } 

And yes, you need a handler. If you just run setCurrent commands from onRestoreInstanceState, android will just override you.

0
source share

I see that this problem was active earlier, but I solved the problem, and if someone has this problem, it is resolved: Everything that does onResume () first gets the value for an hour, then sets the hour in timePicker to hour + 1, and then set to the hour. This works for me.

0
source share

Crazy solution that works:

  • Run AsynTask in onResume ()
  • 50 ms sleep
  • Return to UI thread using Activity.runOnUiThread ()
  • In the user interface flow settings, min / hour to 0
  • Then set min / hour to your values

Here's the gist with code ready for copy-paste :)
https://gist.github.com/sorrro/9415702

0
source share

All Articles