旋转设备时保存缓存
我有一个 Gallerywiew
。
我使用lazyload
来下载图像,但是当我旋转设备时,它会重新加载所有图像并且不使用缓存。
如果我这样做 android:configChanges="keyboardHidden|orientation"
当前图像的大小为最新方向。
为了让图像显示完整尺寸,我这样做:
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不鼓励重写 onConfigurationChanged(),因为您需要做很多工作才能使其正确。
您想要做的是在您的活动中实现 onRetainNonConfigurationInstance() 。当系统知道它将立即重新启动它(例如,用于屏幕旋转)时,会在您的 Activity 被终止之前调用此函数。
您的 onRetainNonConfigurationInstance() 实现可能会返回它喜欢的任何对象(“this”是一个不错的选择,或者在您的情况下是您的缓存)。该对象将被保存并可供您的下一次活动调用。
在 onCreate() 方法中,调用 getLastNonConfigurationInstance() 来检索系统为您保存的对象。如果此函数返回 null,则按正常方式继续。如果它返回非 null,那么这将是您之前从 onRetainNonConfigurationInstance() 传回的对象,您可以从中提取您想要的任何信息。这通常意味着您不需要 savingInstanceState 包或保存的首选项中的任何内容。
我相信即使是打开的套接字、正在运行的线程和其他对象也可以通过这种方式在配置更改中保留。
添加:如果您确实通过 onRetaineNonConfigurationInstance() 传递
this
,请不要保留它。您将阻止释放大量资源。提取你需要的信息然后发布。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.
Adding on: If you do pass
this
through onRetaineNonConfigurationInstance(), don't hang on to it. You'll keep huge amounts of resources from being freed. Extract the information you need and then release it.您需要重写 onConfigurationChanged 事件。
在我的应用程序中,我正在播放音乐,当你翻转手机时,它会停止,然后我发现,如果我覆盖配置更改,我可以告诉它再次启动计时器,并且不会丢失任何内容。我完全知道您想要什么,但它可能会帮助您朝着正确的方向前进。
You need to override your onConfigurationChanged event.
In my app I was playing music and when you flipped the phone it would stop and then I found out that if I overrode the config change I could just tell it to start my timer again and nothing was lost. I know this inst exactly what you wanted but it may help get you going in the right direction.
当您旋转屏幕时,您将经历 Activity 生命周期。该活动将执行 onDestroy() 并调用其 onCreate() 方法。您必须在 onCreate 中重做所有显示计算和视图更改,并使用 savingInstanceState 来保存应用程序数据。
我希望这就是你要问的
When you rotate the screen, you go through the activity lifecycle. The activity goes through onDestroy() and its onCreate() method gets called. You must redo any display calculations and view changes in onCreate and use the savedInstanceState to persist app data.
I hope this is what you were asking