简体   繁体   中英

Android:Save recyclerview's data to avoid network calls

I have a RecyclerView in my app.When an item is clicked from RecyclerView it opens new activity.

New activity contains two fragments both the fragments loads data from network call. I've used static variables to store data so that when a fragment is opened next time it will not make network call.This works fine but when i click other item from RecyclerView the new activity shows fragment containing data of the previously selected item as the variables are static. How to avoid this?

如果你有id,在那些recyclerview项目的某个地方,你可以做的只是在具有该ID的SharedPreference或SQlite数据库中保存数据,并且每当你需要回调它时,从ID本身调用。

The best would be to use Database, but if you don't want to do that or data is changing after certain time, you can use the LruCache class provided in android and use item id as key. This gives you control to how you want to store item as well as saving memory by removing least recently used item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM