简体   繁体   English

如何在活动之间传递大型集合(Master-Detail流程)

[英]How to pass a large collection between Activities (Master-Detail flow)

Background: 背景:

I'm implementing an app that reads info about movies from a web service. 我正在实现一个应用程序,它可以从Web服务中读取有关电影的信息。 That web service return several info about each movie (title,date,poster url, director, actors, etc). 该Web服务返回有关每部电影的几个信息(标题,日期,海报网址,导演,演员等)。

That web service supports pagination, so the movies are loaded in packs of 100. 该Web服务支持分页,因此电影以100个一包的形式加载。

Implementation: 执行:

The idea is to show a grid with all the posters. 这个想法是显示一个包含所有海报的网格。 Auto requesting more items when the user scrolls down. 当用户向下滚动时自动请求更多项目。

在此输入图像描述

When an item is clicked the user navigates to a gallery with the detail view of the selected movie, allowing the scroll through the details with a ViewPager. 单击某个项目时,用户将导航到包含所选影片详细信息视图的库,允许使用ViewPager滚动详细信息。

在此输入图像描述

So the idea is to pass the collection of movies retrieved in the grid to the "DetailedGalleryActivity". 因此,我们的想法是将网格中检索到的电影集合传递给“DetailedGalleryActivity”。

UPDATE: Also is necessary to save the state when the user leaves the fragment in order to handle the fragment lifecycle. 更新:当用户离开片段以处理片段生命周期时,还需要保存状态。 You can test it enabling the Developer option: Don't keep activities 您可以测试它启用Developer选项: 不要保留活动

The problem 问题

My first approach was to serialize the collection of movies in a json, and pass it as a String extra to the Activity. 我的第一种方法是在json中序列化电影集合,并将其作为String Extra传递给Activity。

But since the list of movies is big, if the user scrolls a lot in the grid, the size of the json is extremely big for a Bundle (see Max size of string data ), getting runtime exceptions. 但由于电影列表很大,如果用户在网格中滚动很多,则json的大小对于Bundle来说非常大(请参阅字符串数据的最大大小 ),从而获得运行时异常。

I have checked some answers that talks about persist the data in the SharedPreferences or other persistent storage before launching the detail activity and then access to it from the detail. 我已经检查了一些答案,这些答案谈到在启动详细信息活动之前将数据保存在SharedPreferences或其他持久存储中,然后从详细信息中访问它。 I find this solution weird because it ignores the mechanisms to pass data between activities with a custom and handmade solution. 我发现这个解决方案很奇怪,因为它忽略了使用自定义和手工制作的解决方案在活动之间传递数据的机制。

What is the best approach to solve this problem? 解决这个问题的最佳方法是什么?

I am not also sure if this is a good suggestion. 我不确定这是否是一个好建议。

Make another class and add a static method let's name it MovieUtil. 创建另一个类并添加一个静态方法让我们将它命名为MovieUtil。

//sample //样品

private class MovieUtil {
  private static List<Movies> movies;
  public static List<Movies> getMovies();//getter
  public static void setMovies(List<Movie> movies);//setter
}

Before you start the DetailedGalleryActivity set the list of movies on MovieUtil.setMovies() 在启动DetailedGalleryActivity之前,在MovieUtil.setMovies()上设置电影列表

then on oncreate of DetailedGalleryActivity initialize the List by using the MovieUtil.getMovies() 然后在DetailsGalleryActivity的oncreate上使用MovieUtil.getMovies()初始化List

Note: you can clear the list of movies on movieutil when you close the DetailedGalleryActivity. 注意:您可以在关闭DetailedGalleryActivity时清除movieutil上的电影列表。

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

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