android – 在onCreate中检查savedInstanceState是否为null是判断设备是否已旋转的好方法?

只有当它们是第一次构建时,我才想在我的Activities’onCreate()方法中做的事情,而不是在设备被旋转时(在配置更改时).目前我正在检查传入onCreate()的savedInstanceState参数.如果它为null,那么它是Activity第一次启动,否则只有一个旋转.

这是一个好的,可靠的方式来告诉这个吗?有替代品吗?

解决方法

我不知道更好的解决方案. Romain Guy描述了 same approach(检查savedInstance状态或您传递的其他对象为null).

In the new activity,in onCreate(),all you have to do to get your
object back is to call getLastNonConfigurationInstance(). In
Photostream,this method is invoked and if the returned value is not
null,the grid is loaded with the list of photos from the prevIoUs
activity:

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

当我懒得这样做时,我只是禁用在方向更改时重新创建活动.如How do I disable orientation change on Android?所述

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...