0

I have a photo gallery in my application, not too dissimilar from Facebook photos. I have two basic views:

  • PhotoGalleryActivity is an endless list of photos shown chronologically. Tapping into it takes you to a detail view.

  • PhotoDetailView is basically a view pager of the photos and for each photo, it also shows the user that uploaded it. Clicking on the user, takes you to another PhotoGalleryActivity listing all the photos the user uploaded.

To show the PhotoDetailActivity, I need to pass the list of photos from the PhotoGalleryActivity to it. A photo object is basically just a list of URLs and a User Object.

I looked into different possible solutions but none of them fit my bill. Here are the ones I found and why they won't work (please correct me if I am wrong):

  • Intent: This didn't work because the data can get big (1mb is the limit for passing data through intent). I might need to pass 10,000 photos or 100,000 photos. AND because of this, I can't even save it during orientation changes (am I wrong?).

  • Singleton + tags: Since the user can keep clicking through to multiple PhotoGalleryActivity, I can use tags so that the PhotoDetailActivity can just grab the list of photos by using the tag that PhotoGalleryActivity passes through an Intent. But the problem arises when the application is killed. The singleton loses the data and so I can't reliably do this. (I feel like I might be missing a workaround with this use case).

  • Database: I could save them in a database, but this becomes hard to maintain because let's say the first PhotoGalleryActivity shows photos from all users. Then I click on a photo, click on the user that uploaded a photo, and start browsing that users photos. Now when I go back the the the first PhotoGalleryActivity, chronologically the last photo in the database is now the last photo of that user, so my data becomes tainted. (I can think of workarounds for this, but it feels like it can get messy very quickly).

I was hoping someone out there has some experience with this and has an idea for a solution.

Sree
  • 2,727
  • 3
  • 29
  • 47
  • I don't understand why the database approach is a problem. If you are worried about timestamps, you can simply sort by some value to get the ordering back – OneCricketeer Mar 31 '16 at 01:41
  • what do you mean by that? – Sree Mar 31 '16 at 01:45
  • Any popular database has some order by or sort feature... What do you mean by "chronologically the last photo in the database is now the last photo of that user, so my data becomes tainted" – OneCricketeer Mar 31 '16 at 01:50
  • it's basically exactly what you said, I need to fetch photos after the last photo timestamp. It also becomes a problem when trying to fetch photos in photo detail view after "tainting" the database because now I don't know which photos were part of the All Photos list and which were part of the User Photos list – Sree Mar 31 '16 at 01:58
  • I only copied what you typed in your question. I was asking for clarification. To get photos for a User, you would store a User_id with the photos to know which user has which photos. When you add a photo, you store when it was inserted. When you go back to "All photos", just requery the database and refresh the photos – OneCricketeer Mar 31 '16 at 02:03
  • In singleton approach what is your issue with application get killed. I mean do you want to restore the app to the state at which it was killed? – Abdullah Mar 31 '16 at 02:39
  • @cricket_007 hmm lemme think about that – Sree Mar 31 '16 at 02:45
  • @Abdullah yea, that would be the most ideal way – Sree Mar 31 '16 at 02:45

3 Answers3

0

Maybe you can use Service, let activities bind same Service and save data object in Service, then activities can load same data object by Service.

I think Service has no data size limit, more reliable than singleton, flexible than Database.

Peter-Yu
  • 191
  • 7
  • Could you elaborate? – Sree Mar 31 '16 at 01:59
  • There are many tutorials on internet you can Google "Android activity bindService", here are some of them:[bind/unbind service example](http://stackoverflow.com/questions/8341667/bind-unbind-service-example-android), [Bound Service Example In Android](http://www.truiton.com/2014/11/bound-service-example-android/). – Peter-Yu Mar 31 '16 at 02:14
0

I think that the best approach would be to use a database. It will maintain the data and the actions are relatively quick. You can use different kind of ORM libraries such as Realm to help you easier interface to the database. Than you will be able to easily query whichever data you need in each page (I assume you will use a view pager for the gallery.

ranchuk
  • 83
  • 2
  • 4
  • It's not quite so simple. If there was only a single gallery it's fine. But consider this : start the app, opens main gallery which shows All Photos, tap a photo to go to the detail view, tap the user which opens another gallery, User Photos. Now the last photo (by timestamp) in the database is the last photo that was fetched when we were in the user's gallery – Sree Mar 31 '16 at 11:41
0

I would suggest you to create a text file in your app folder with the Uri's of photos you want to send to next Activity and then retrieve them in the next Activity. If in future you don't want user to access the text file you can easily encrypt it using your own algo, it wont take much time or effort to implement this solution.

Nanites
  • 410
  • 3
  • 16