首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

银幕切换

2012-09-23 
屏幕切换android切屏生命周期:http://marshal.easymorse.com/archives/2056禁止切屏时调用oncreate,onstop

屏幕切换
android切屏生命周期:
http://marshal.easymorse.com/archives/2056

禁止切屏时调用oncreate,onstop,onresume
如果在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
当屏幕翻转时,Activity就不会重复的调用onCreate()、onPause()和onResume().
而是调用onConfigurationChanged(Configuration newConfig)
http://stulog.com/?post=214

在android官方文档中指出另一种保存数据的方法:
The Activity class has a special method called onRetainNonConfigurationInstance(). This method can be used to pass an arbitrary object your future self and Android is smart enough to call this method only when needed. In the case of Photostream, the application used this method to pass the downloaded images to the future activity on orientation change. The implementation can be summarized like so:

private void loadPhotos() {    final Object data = getLastNonConfigurationInstance();        // The activity is starting for the first time, load the photos from Flickr    if (data == null) {        mTask = new GetPhotoListTask().execute(mCurrentPage);    } else {        // The activity was destroyed/created automatically, populate the grid        // of photos with the images loaded by the previous activity        final LoadedPhoto[] photos = (LoadedPhoto[]) data;        for (LoadedPhoto photo : photos) {            addPhoto(photo);        }    }}

Be very careful with the object you pass through onRetainNonConfigurationChange(), though. If the object you pass is for some reason tied to the Activity/Context, you will leak all the views and resources of the activity. This means you should never pass a View, a Drawable, an Adapter, etc. Photostream for instance extracts the bitmaps from the drawables and pass the bitmaps only, not the drawables. Finally, remember that onRetainNonConfigurationChange() should be used only to retain data that is expensive to load. Otherwise, keep it simple and let Android do everything.

热点排行