保存缓存时旋转设备 [英] Save cache when rotate device

查看:91
本文介绍了保存缓存时旋转设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Gallerywiew

我用 lazyload 来下载图像,但是当我旋转设备重新加载的所有图像,并且不使用缓存。

如果我这样做机器人:configChanges =keyboardHidden |定位当前图像的最新方向尺寸

要获得的图像显示全尺寸我做的:

 显示显示=((窗口管理器)getSystemService(Context.WINDOW_SERVICE))
                           .getDefaultDisplay();
INT宽度= display.getWidth();
INT高= display.getHeight();
 

解决方案

重写onConfigurationChanged()不提倡,因为有这么多的工作,你必须做的就是它的权利。

您要做的就是实现onRetainNonConfigurationInstance()在你的活动。这就是所谓的之前,当系统知道其将要重新启动它在某一时刻(如屏幕旋转)你的活动被杀害。

您实现onRetainNonConfigurationInstance的()可能会返回它喜欢的任何对象(这是一个不错的选择,或者你的情况,你的缓存)。这个对象将举行,并提供给您的活动的下一次调用。

在您的onCreate()方法,调用getLastNonConfigurationInstance()来检索系统节省你的对象。如果这个函数返回空值,按你通常会。如果返回非空的,那么这将是你previously传回来自onRetainNonConfigurationInstance()的对象,你可以提取你想要的任何信息。这通常意味着你不从savedInstanceState束或保存preferences需要什么。

我相信连开插口,运行的线程,和其他对象可以是整个配置pserved $ P $改变了这种方式。

I have a Gallerywiew.

I'm using lazyload to download images but when I rotate device it reloads all images and does not use the cache.

If I do android:configChanges="keyboardHidden|orientation" the current images are in size of latest orientation.

To get the images to show full size I do:

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
                           .getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

解决方案

Overriding onConfigurationChanged() is discouraged because there's so much work you have to do to get it right.

What you want to do is implement onRetainNonConfigurationInstance() in your activity. This is called just before your activity is killed when the system knows it will be restarting it in a moment (e.g. for screen rotation).

Your implementation of onRetainNonConfigurationInstance() may return any object it likes ('this' is a good choice, or in your case, your cache). This object will be held and made available to the next invocation of your activity.

In your onCreate() method, call getLastNonConfigurationInstance() to retrieve the object the system is saving for you. If this function returns null, proceed as you would normally. If it returns non-null, then that will be the object you previously passed back from onRetainNonConfigurationInstance() and you can extract any information you want from it. This generally means that you don't need anything from the savedInstanceState bundle or from saved preferences.

I believe even open sockets, running threads, and other objects can be preserved across configuration changes this way.

这篇关于保存缓存时旋转设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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