9

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?

Community
  • 1
  • 1
Addev
  • 31,819
  • 51
  • 183
  • 302
  • You can store the json of each movie in a file (movie_1, movie_2, etc) and then pass just the id of the movie and take the data of the current movie from file. Probably, deleting all these files when you are done, should be a good idea. – GVillani82 Aug 27 '15 at 08:11
  • Is there a particular reason that you want to use activities rather than fragments? If you use fragments you can simply load DetailedGallery as a fragment and pass the data as a parameter. – MercilessMaverick Aug 27 '15 at 08:14

1 Answers1

5

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.

ville101
  • 226
  • 1
  • 9
  • This is a good idea - the technique appears in the answer to the following question under 'Use a singleton class' http://stackoverflow.com/questions/4878159/android-whats-the-best-way-to-share-data-between-activities – MercilessMaverick Aug 27 '15 at 08:24
  • i do not also know which is better singleton or static method, there are a lot of people argue which is really better. but i use static method in this kind of scenario for simplicity sake(?).. :) – ville101 Aug 27 '15 at 08:36
  • I updated the question, since this solution solves the pass of data between activities but doesn't work to save info onSaveInstanceState() in order to resume the interface if the activity is destroyed – Addev Aug 27 '15 at 09:00