简体   繁体   English

在活动之间进行切换时保存和还原RecyclerView

[英]Saving and Restoring RecyclerView when switch between activity

I have HomeActivity contain recyclerView show data load from Firebase, but when click items and back HomeActivity then app reload data from Firebase. 我有HomeActivity包含从Firebase加载recyclerView显示数据,但是当单击项目并返回HomeActivity时,应用程序将从Firebase重新加载数据。 I want holder data when back HomeActivity (list data put into CustomAdapter). 我要回HomeActivity时持有人数据(将列表数据放入CustomAdapter)。

If you are accessing the Homeactivity again via startActivity() , or calling finish() or onDestroy() in the Homeactivity There are 3 options: 如果要通过startActivity()再次访问Homeactivity,或在Homeactivity中调用finish()onDestroy() ,则有3个选项:

  1. You need to save the data in a database with the help of SqliteOpenHelper . 您需要借助SqliteOpenHelper将数据保存在数据库中。 Display data by extracting it from the database instead of Firebase 通过从数据库而不是Firebase中提取数据来显示数据
  2. Save data in cache. 将数据保存在缓存中。 Cached data can be re-accessed without storing it. 缓存的数据可以不存储而重新访问。 Cache duration and size would have to be determined by you. 缓存持续时间和大小必须由您确定。
  3. Store data in a shared preference which can be accessed when the activity is opened again. 将数据存储在共享的首选项中,当活动再次打开时可以访问。

When using any of the three methods, you need to specify when the app should connect with firbase to get new data. 使用这三种方法中的任何一种时,您需要指定应用程序应何时与firbase连接以获取新数据。 You can set up a gcm notification to tell the app new data is available or schedule a time after which the app will try to check if new data is available and try to download it. 您可以设置gcm通知以告知应用新数据可用,或安排一个时间,在此之后应用将尝试检查新数据是否可用并尝试下载。

HomeActivity will by default be in the activityStack, so if a backpress is pressed in NextActivity, HomeActivity will open as such. 默认情况下,HomeActivity将位于activityStack中,因此,如果在NextActivity中按下了backpress,则HomeActivity将照此打开。

savedInstanceState works for configuration change (ie rotation etc.). savedInstanceState用于配置更改(即旋转等)。 You will need to override OnSaveInstanceState , to prevent OnCreate to from removing all the data. 您将需要重写OnSaveInstanceState ,以防止OnCreate删除所有数据。 But if you are accessing the Homeactivity again via startActivity() , or calling finish() or onDestroy() inside HomeActivity then this data is lost. 但是,如果您通过startActivity()或在HomeActivity中调用finish()onDestroy()再次访问Homeactivity,则此数据将丢失。

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

相关问题 保存和还原活动状态? - Saving and restoring activity state? 使用GridLayoutManager保存和恢复recyclerview的状态 - Saving and restoring state of recyclerview with GridLayoutManager Android从主屏幕还原应用程序时,切换到其他活动 - Android When restoring app from homescreen, switch to different activity Android:纵向和横向切换时保存和恢复活动数据 - Android : Saving and Restoring Activity data when switching Portrait and Landscape 如何通过单击两个按钮在单个活动中在RecyclerView和ListView之间切换? - How to switch between RecyclerView & ListView in single activity with two buttons clicks? 保存和恢复 FirebaseUI RecyclerView 滚动状态 - Saving and Restoring FirebaseUI RecyclerView scroll state Recyclerview 中的 switch case 问题,因为它在过滤项目时打开了错误的活动 - Issue with switch case in Recyclerview as it opens wrong activity when items are filtered 旋转时保存和还原文本 - Saving and restoring text when rotating RecyclerView:获取位置并切换到活动 - RecyclerView : get position and switch to activity 单击AlertDialog项目列表时,在RecyclerView布局之间切换 - Switch between RecyclerView layouts when click on AlertDialog item list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM