简体   繁体   English

Android在屏幕旋转期间保存活动数据

[英]Android save Activity data during screen rotation

I google a lot, but now I decided to ask.. 我在Google上搜索了很多,但现在我决定问..

My activity does only one simple thing - it downloads a picture from Internet using AsyncTask. 我的活动仅做一件简单的事情-它使用AsyncTask从Internet下载图片。

But there is a lot of problems, that I am not sure, how to solve.. 但是我不确定如何解决还有很多问题。

1.] User rotates screen after AsyncTask have downloaded the picture.. How should I handle this screen rotation? 1.] AsyncTask下载图片后,用户旋转屏幕。我该如何处理屏幕旋转? Where to temporary save picture to be available after activity recreates? 重新创建活动后可以在哪里临时保存图片?

I don't want to lock screen orientation (android:screenOrientation="landscape") or handle it by myself (android:configChanges="orientation").. 不想锁定屏幕方向(机器人:screenOrientation =“风景”)或自己处理它(机器人:configChanges =“方向”)..

I just want to save the picture somewhere.. 我只想将图片保存在某个地方。

I know It can be done by using onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() , but both are deprecated now. 我知道可以使用onRetainNonConfigurationInstance()getLastNonConfigurationInstance()来完成 ,但现在都已弃用 Bundle isn't appropriate for images, because it is made for String (Serializable) data. Bundle不适合图像,因为它是为String(可序列化)数据制作的。 I can save picture to database or to file somewhere, but it is unnecessary. 我可以将图片保存到数据库或文件中的某处,但这不是必需的。 I read somewhere, that it can be done by Loader, but I don't know how? 我在某个地方读到它可以由Loader完成,但是我不知道怎么办? Is it possible? 可能吗?

2.] User rotates screen during AsyncTask is downloading a picture. 2.] AsyncTask下载图片期间,用户旋转屏幕。

I want AsyncTask to continue with downloading. 我希望AsyncTask继续下载。 After download is complete, AsyncTask should save image to new (recreated) activity in onPostExecute method. 下载完成后,AsyncTask应该通过onPostExecute方法将图像保存到新的(重新创建的)活动中。

I think if AsyncTask is inner class of activity, it works..? 我认为,如果AsyncTask是活动的内部类,它就可以工作。 But if I don't want it to be inner class I have to save reference to activity, for example in variable. 但是,如果我不希望它成为内部类,则必须保存对活动的引用,例如在变量中。 But if I send reference to Activity as a parameter of constructor of AsyncTask, and Activity is recreated after screen rotation, the variable in AsyncTask does refer to original activity and so new one doesn't know about downloaded picture and it has to start download again.. 但是,如果我将对Activity的引用作为AsyncTask构造函数的参数发送,并且在旋转屏幕后重新创建Activity,则AsyncTask中的变量确实引用了原始活动,因此新的人不知道下载的图片,因此必须重新开始下载..

Or is AsyncTask bad choice for downloading image? 还是AsyncTask是下载映像的错误选择? Should I use something else? 我应该使用其他东西吗? Service? 服务?

So.. How to solve these problems? 那么..如何解决这些问题? I am sure a lot of app developers have to solve this too.. But it is all Greek to me:( 我相信很多应用程序开发人员也必须解决这个问题。

When your AsyncTask has finished downloading the image, save it to your application's cache directory. 当您的AsyncTask完成下载映像后,将其保存到应用程序的缓存目录中。 Persist the path to the saved image in a property on your Activity class. 在Activity类的一个属性中保留指向保存的图像的路径。 You can then override the onSaveInstanceState method of your activity to pass the cached image path to itself during an orientation change. 然后,您可以覆盖活动的onSaveInstanceState方法,以在方向更改期间将缓存的图像路径传递给自身。 In your onCreate method check the savedInstanceState bundle for a saved image path. 在您的onCreate方法中,检查savedInstanceState捆绑包是否有保存的图像路径。 If that path exists, do not attempt to re-download the image. 如果该路径存在,请不要尝试重新下载映像。 Instead, simply load the image from disk. 相反,只需从磁盘加载映像。

I've already answered somewhere. 我已经在某个地方回答了。 I found a very nice example: read this . 我发现了一个很不错的例子:读这个 This example for old APIs. 此示例适用于旧的API。

For new you can use setRetainInstance method of a fragment (if you use fragments). 对于新版本,可以使用片段的setRetainInstance方法(如果使用片段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中的屏幕旋转中保存片段中的数据 - Save data in Fragments on screen rotation in Android Google Maps API如何在旋转屏幕期间保存数据? - Google maps api how to save data during rotation screen? 如何在屏幕旋转过程中将对话框的参考保存/恢复(更新)?(我需要在onCreate方法中使用ref)。 - How to save/restore(update) ref to the dialog during screen rotation?(I need ref in onCreate method of activity.) 在屏幕旋转期间保存对象的状态 - save the object's state during screen rotation 在屏幕旋转期间保存BottomNavigationView所选项目 - Save BottomNavigationView selected item during screen rotation 如何在屏幕旋转期间保存 MutableList 的状态 - How to save the state of MutableList during screen rotation 屏幕旋转后,Android从嵌套的AsyncTask onPostExecute保存数据 - Android save data from nested AsyncTask onPostExecute after screen rotation Android活动能够在屏幕旋转中幸存下来,但不能最小化 - Android activity able to survive screen rotation but not minimizing Android如何禁用Activity而不是菜单上的屏幕旋转 - Android how to disable screen rotation on Activity, but not the menu 活动通过屏幕旋转重新启动我的游戏(Android) - Activity restarts my game on screen rotation (Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM