Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TimePicker hour field disappears after orientation change

Tags:

android

I am using a TimePicker under API 1.5 and when the orientation is changed on my device (a stock G1 running 1.6 - though NOT on the 1.5/1.6 emulator), the hour field goes blank. It still remembers the hour, it just doesn't show it. Is there any workaround for this?

The same problem was described by someone else here:

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

Nothing helpful was forthcoming - can StackOverflow do better?

like image 943
sosiouxme Avatar asked Jan 31 '10 03:01

sosiouxme


3 Answers

Simply move invoking of methods TimePicker.setCurrentHour and TimePicker.setCurrentMinute to onResume method of your Activity.

like image 163
kulikoff Avatar answered Nov 07 '22 03:11

kulikoff


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

   @Override
    public void onResume() {
        super.onResume();

        TimePicker tmp = (TimePicker) this.findViewById(R.id.yourTimePicker);
        tmp.setCurrentHour(7); // This will also set on rotate

    }
like image 43
Ricky Avatar answered Nov 07 '22 04:11

Ricky


Take a look at this: http://code.google.com/p/android/issues/detail?id=22824 No matter what you do you won't be able to fix the issue without destroying the Activity and recreating it when the rotate occurs. In order to not enter an endless loop simple check to see if the savedInstanceState != null in the onCreate. Something like this will work:

if (savedInstanceState!=null){
    Intent myIntent = 
        new Intent(getApplicationContext(), CurrentClassName.class);
    startActivityForResult(myIntent, 0);  
    finish();
} 
like image 1
DanRoss Avatar answered Nov 07 '22 02:11

DanRoss