Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. That web service return several info about each movie (title,date,poster url, director, actors, etc).

That web service supports pagination, so the movies are loaded in packs of 100.

Implementation:

The idea is to show a grid with all the posters. Auto requesting more items when the user scrolls down.

enter image description here

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.

enter image description here

So the idea is to pass the collection of movies retrieved in the grid to the "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

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.

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.

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. 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?

like image 604
Addev Avatar asked Aug 27 '15 08:08

Addev


1 Answers

I am not also sure if this is a good suggestion.

Make another class and add a static method let's name it 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()

then on oncreate of DetailedGalleryActivity initialize the List by using the MovieUtil.getMovies()

Note: you can clear the list of movies on movieutil when you close the DetailedGalleryActivity.

like image 97
ville101 Avatar answered Nov 20 '22 14:11

ville101