简体   繁体   中英

How to maintain DatePicKer State After Rotate Screen

I have implemented the Demo version of HelloDatePicker provided by the API.

http://developer.android.com/resources/tutorials/views/hello-datepicker.html

However I cannot maintain its value after rotating the screen. Can someone post an example or indicate how one can do this.

Thanks

使用onRetainNonConfigurationInstance()保存日期,在onCreate中使用getLastNonConfigurationInstance()加载和设置日期。

After rotation, Activity is recreated. The simplest way to avoid it is to add:

android:configChanges="keyboardHidden|orientation"

in AndroidManifest.xml in Activity declaration.

For more information you should read it .

Even without a Fragment, the Activity alone can do this:

@Override protected void onSaveInstanceState (Bundle outState)
{
    super.onSaveInstanceState (outState);
    outState.putInt ("YEAR", mDatePicker.getYear ());
    outState.putInt ("MONTH", mDatePicker.getMonth ());
    outState.putInt ("DAY", mDatePicker.getDayOfMonth ());
}

@Override protected void onRestoreInstanceState (Bundle savedInstanceState)
{
    super.onRestoreInstanceState (savedInstanceState);
    mDatePicker.updateDate (savedInstanceState.getInt ("YEAR"),
                            savedInstanceState.getInt ("MONTH"),
                            savedInstanceState.getInt ("DAY"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM