在 Android 中保存方向变化的一些数据

发布于 2024-12-01 05:08:35 字数 114 浏览 1 评论 0原文

据我了解,任何方向更改都会针对新方向重新创建您的 Android 活动。

有没有办法在方向改变时存储/保存原始方向的一些数据?

我想存储一些位图,这样我就不必在方向更改时再次加载它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚你发端 2024-12-08 05:08:35

就可维护性和调试而言,使用静态变量/类是一种不好的方法。


我一直在使用 Activity.onRetainNonConfigurationInstance ,但我现在发现它已被弃用(可能是从 honeycomb 或更高版本开始)。
Activity.onRetainNonConfigurationInstance

使用此方法,只需调用Activity。 getLastNonConfigurationInstance 来检索您在 onRetainNonConfigurationInstance 中返回的同一对象。请务必检查 null 并强制转换为正确的类(您可以返回/获取任何类)。 Activity.getLastNonConfigurationInstance

伪代码中的示例用法如下

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,则可以使用该方法。否则,Google 建议您使用 Fragment.setRetainInstance。这是通过兼容包向后兼容的。

Fragment.setRetainInstance

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


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

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

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

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.

Fragment.setRetainInstance

遇到 2024-12-08 05:08:35

将项目保存在父活动或静态实用程序类中。

否则,您可以使用清单告诉应用程序不要破坏屏幕大小调整时的活动。查看这篇文章:http://developer.android.com/guide/topics/resources/runtime -changes.html

Save the items in a parent activity or static utility class.

Otherwise, you can use the manifest to tell the app to not destroy the activity on screen resizes. Check out this article: http://developer.android.com/guide/topics/resources/runtime-changes.html

少年亿悲伤 2024-12-08 05:08:35

实际上,Android 开发者网站上有一篇非常好的文章涵盖了这个主题。

某些设备(例如 T-Mobile G1)可以更改其硬件
运行时配置。例如,当您打开键盘时,
屏幕从纵向变为横向
方向。

为了让Android应用开发更加简单,Android系统
自动处理配置更改事件并重新启动
使用新配置的当前活动。

[...]

虽然这种行为非常强大,因为您的应用程序会适应
在运行时自动到设备的配置,它是
有时,新的 Android 开发者会感到困惑,他们想知道为什么他们的
Activity 被销毁并重新创建。

面对这个“问题”,一些开发人员选择处理配置
改变自己,一般来说,这是一个短期解决方案
只会让他们以后的生活变得复杂。

[...]

全文

Actually there is a very nice article on the Android Developer site which covers this topic.

Some devices, like the T-Mobile G1, can change their hardware
configuration at runtime. For instance, when you open the keyboard,
the screen change from the portrait orientation to the landscape
orientation.

To make Android app development easier, the Android system
automatically handles configuration change events and restarts the
current activity with the new configuration.

[...]

While this behavior is really powerful, since your application adapts
automatically to the device's configuration at runtime, it is
sometimes confusing for new Android developers, who wonder why their
activity is destroyed and recreated.

Facing this "issue," some developers choose to handle configuration
changes themselves which is, in general, a short-term solution that
will only complicate their lives later.

[...]

The full article.

疧_╮線 2024-12-08 05:08:35

运行您的活动的进程将不会重新启动。 Android 框架只会创建您的 Activity 的一个新实例。因此,作为最简单的解决方案,您可以将数据存储在静态变量中。

Process running your activity will not be restarted. Android framework will just create a new instance of your activity. So as simplest solution you can store your data in static variables.

夏至、离别 2024-12-08 05:08:35

您可以尝试使用共享首选项:

editor edit =preferences.edit();
edit.putString("用户名", "new_value_for_user");
编辑.commit();

you can try using sharedpreferences:

editor edit = preferences.edit();
edit.putString("username", "new_value_for_user");
edit.commit();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文