开发者

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.htm开发者_如何学Cl

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

Thanks


Use onRetainNonConfigurationInstance() to save the date, and in onCreate use getLastNonConfigurationInstance() to load and set the date.


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"));
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜