Android content saving

随声附和 提交于 2019-12-24 19:25:38

问题


I have a weird problem. As I have asked in previous posts, I have a lot of data coming from NET in my app. The problem is that a lot of text and images r there, so though I can make the user wait wen he clicks on a list item, but I don't want to make him wait wen he clicks the back button.

In the sense that I don't want to re-download all data; data 1ce downloaded should remain.

Which 1 is the right method?

1. Using a special class which stores all data... and using the variables each time I go back,

or

2. Is there any specific save instance method for saving such data in the class itself... if it is then do inform me how to call the saved instance Activity when back is clicked.


回答1:


By "user clicks the back button" I understand that the previous activity is displayed and returned at the top of the stack (see activity lifecycle).

This mean this activity is not created again, but resumed (unless it was killed by core because other applications need memory), so its fields and attributes keep their values. So if you store your datas in some activity fields, you might be able to retrieve their values and display them without re-download them.

Of course you have to test these fields before accessing them in order to avoid NPE in case of activity first start.




回答2:


You shouldn't rely on any Activity already existing when you start it or return to it.

Check out this previous question about caching already-downloaded data:
What is the ideal place to cache images?

You shouldn't have any pauses in the UI anyway when the user enters an Activity; you should be doing all the downloading on a background thread and showing some sort of progress indicator to the user. If you don't do this and the UI thread is blocked by a slow download (about 15 seconds), the user will be presented with an "Application Not Responding" dialog and asked to either kill your application or keep waiting. Not good.



来源:https://stackoverflow.com/questions/1984682/android-content-saving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!