I have a very specific use case I want to implement but I am not quite sure how to do it.
Scenario:
I have an activity (call it ActivityA) which loads (at most 10) images from specific URLs and shows them in a custom ViewPager. I created a class (lets say AttributedPhotos) that holdes the bitmaps and the the attribution of it. This class implements Parcelable.
Then I registered an OnTouchListener to this ViewPager so the images are opened in another Activity in fullscreen if the user touches the ViewPager and the user can navigate through them by swiping left and right (and pinch to zoom and so forth, basically interact with them in fullscreen). At first I tried to pass the images as an ArrayList of AttributedPhotos (intent.putParcelableArrayListExtra("attr-photos", myPhotos)) to the next Activity but then got the error message E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! which apparently means that there is not enough space in the Intent for this ArrayList. Which makes sense because the ArrayList contains 10 500x500 bitmaps. So I searched in StackOverflow and found a solution to my problem here which is basically storing the images in the device memory in ActivityA and loading them in ActivityB. I implemented helper classes that read/write the images from/to the cache directory asynchronously (ImageLoader and ImageSaver, both with static methods). This seems to work for me BUT I want ActivityB to only load the images if ActivityA has finished saving them and that's where my problem is.
Question
Is there a way to notify ActivityB that ActivityA finished writing the images to the cache? I thought of something like:
Activity A:
ImageSaver saver = new ImageSaver(...);
saver.setCallback(activityB);
saver.execute();
startActivity(activityB);
Activity B implements OnSavedListener:
@Override
public void onImagesSaved() {
ImageLoader loader = new ImageLoader(viewPager,...); // ViewPager instance to set the images after loading (in onPostExecute())
loader.execute();
}
Brief explanation: I want activity A to set activity B as a callback object so that ImageSaver calls activityB if all images are saved. Meanwhile activityA started activityB (which shows a progress spinner until the images are loaded) ... or something similar to this.
For now I send activityB the size of the arraylist via intent and implemented a method to check if the number of files in the appropriate cache-subdirectory equals the size of the arraylist of AttributedPhotos in activityB and call this method every second. Only if the number of files equals the size of arraylist, the images are loaded. This is obviously a dirty hack. At least for me it seems like a dirty hack. One problem with this solution is that if the last bitmap needs for example 5 seconds to be written then activityB cant read the last image because the number of files is correct as soon as the last file is created but as reading is much faster than writing, activityB could read all files before the last one had been written completely.
I would like to have a more robust solution if possible. Preferably something like the pseudo code above.
Thank you in advance!