(There are several similiar questions like How can I refresh a previous activity after the back button is pressed? or Back button and refreshing previous activity, which do not exactly satisfy my needs.)
I'm having two activities:
- PhotoListActivity: This activity contains a
RecyclerView
and aPhotoListAdapter<ArrayList<PhotoModel>>
which displays multiple photos. - PhotoDetailActivity: This activity displays a single photo and it details.
The problem: The user starts at the PhotoListActivity and ends up in the PhotoDetailActivity by clicking a photo. Both activities allow users to like a photo. Liking a photo will result in a request to the server, where the like is saved. When the user now likes a photo in the PhotoDetailActivity and presses the back button, the PhotoListActivity and its corresponding PhotoListAdapter<ArrayList<PhotoModel>>
doesn't know anything about this change, because the data of the adapter still contains the "not-liked" photo.
What is the preferred android way to display the updated data?
- Reload the data for the
PhotoListAdapter<ArrayList<PhotoModel>>
from the server in theonResume
/onRestart
method - Pass back the updated
PhotoModel
from the PhotoDetailActivity, loop through thePhotoListAdapter<ArrayList<PhotoModel>>
's ArrayList and update the correspondingPhotoModel
. - Have a local cache (either way static or in SharedPrefs) which holds a list of liked photos. Everytime a user like/unlikes a photo, I could update that cache. Then the only thing I have to do now in my PhotoListActivity, is to check if a cache entry for the given photo is set and use the like value for that, instead of the one from the
PhotoModel
. The advantage of that would be, that I don't have to pass around my updatedPhotoModel
all the time.
The second solution is obviously preferable, because we do not have to make another server request, but it seems kind of "hacky" to loop through the PhotoListAdapter<ArrayList<PhotoModel>>
and update the matching PhotoModel
.
Is there any better way to solve that in android?
Edit: Updated and added a third possible solution.