微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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?所述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐