节约的方向变化,Android的一些数据 [英] Saving some data on orientation change in Android

查看:100
本文介绍了节约的方向变化,Android的一些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,你的Andr​​oid活动将重新创建可在任何方向改变的新方向。

As far as I've understood, your Android activity will be recreated for the new orientation on any orientation changes.

有没有一种方法来存储/保存一些数据来自于方向的改变原来的方向?

Is there a way to store / save some of the data from the original orientation upon the orientation change?

我想储存一些位图,这样我就不必再重新装上的​​方向变化。

I'd like to store some Bitmaps, so I don't have to load it again on the orientation change.

推荐答案

使用静态变量/班是在维护和调试方面有不错的办法。

Using static variables/classes is a bad approach in terms of maintainability and debugging.

我一直在使用 Activity.onRetainNonConfigurationInstance 但是我发现刚才这是pcated(可能是因为蜂窝或更高版本)德$ P $。 <一href="http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29">Activity.onRetainNonConfigurationInstance

I have been using Activity.onRetainNonConfigurationInstance but I found out just now that this is deprecated (probably since honeycomb or later). Activity.onRetainNonConfigurationInstance

使用这种方法,只需拨打 Activity.getLastNonConfigurationInstance 来检索您在 onRetainNonConfigurationInstance 返回同一个对象。一定要检查空,并转换为正确的类(你可以返回/得到任何类)。 <一href="http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance%28%29">Activity.getLastNonConfigurationInstance

Using this method, just call Activity.getLastNonConfigurationInstance to retrieve the same object you returned in the onRetainNonConfigurationInstance. Be sure to check for null and cast to the right class (you can return/get any class). Activity.getLastNonConfigurationInstance

在伪code的样本用法是:

A sample usage in pseudo-code would be:

onRetainNonConfigurationInstance:
    return "I need to remember this next time";

onCreate:
    ...
    String messageToShow = null;
    Object data = getLastNonConfigurationInstance();
    if(data != null)
        messageToShow = (String)data;
    else
        messageToShow = "Nothing to show";


所以,如果你是打靶高达2.xx的,你可以使用该方法。否则,谷歌建议您使用 Fragment.setRetainInstance 。这是通过compability包向后兼容。


So, if you are targetting up to 2.x.x you can use that method. Otherwise, google recommends you to use Fragment.setRetainInstance. This is backwards compatible via the compability package.

<一个href="http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29">Fragment.setRetainInstance

这篇关于节约的方向变化,Android的一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆