1

(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 a PhotoListAdapter<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?

  1. Reload the data for the PhotoListAdapter<ArrayList<PhotoModel>> from the server in the onResume/onRestart method
  2. Pass back the updated PhotoModel from the PhotoDetailActivity, loop through the PhotoListAdapter<ArrayList<PhotoModel>>'s ArrayList and update the corresponding PhotoModel.
  3. 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 updated PhotoModel 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.

Community
  • 1
  • 1
Chris
  • 4,255
  • 7
  • 42
  • 83

1 Answers1

1

I think I would combine solution number 2 with @Rajesh solution. You don't have to loop through the list: If you pass the position index alongside the PhotoModel, you will be able to know exactly which position to update in your ArrayList.

Step 1

Inside PhotoListActivity, Start PhotoDetailActivity and pass along the PhotoModel & its position in your ArrayList.

Intent intent = new Intent(this, PhotoDetailActivity.class);
intent.putExtra("photoModel", myPhotoModel);
intent.putExtra("photoModelPosition", position); // = represents the index of array
startActivityForResult(intent, PHOTO_DETAIL_ACTIVITY); //int const that you choose

Step 2

Inside PhotoDetailActivity, when the user finishes / presses back / whatever your condition is, prepare the updated PhotoModel and the position info to be returned to the calling activity:

if (photoModelChanged) { //Your condition here..
    int originalPos = getIntent().getExtras().getInt("photoModelPosition");

    Intent intent = new Intent();
    intent.putExtra("photoModel", updatedPhotoModel);
    intent.putExtra("photoModelPosition", originalPos);
    setResult(RESULT_OK, intent);
    finish();
}

Step 3

Inside PhotoListActivity, override onActivityResult and implement the updated PhotoModel into your existing ArrayList. You can then call your adapter's notifyItemChanged if you wish to:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_DETAIL_ACTIVITY) {
        if (resultCode == RESULT_OK) {
            PhotoModel updatedModel = data.getExtras()
                   .getSerializable("photoModel"); //I assume your class is serializable?
            int pos = data.getExtras().getInt("photoModelPosition");

            //Update the activity's ArrayList<PhotoModel>
            this.arrayList.set(pos, updatedModel);

            //Notify the PhotoListAdapter with the change
            this.adapter.notifyItemChanged(pos);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Community
  • 1
  • 1
Barak
  • 1,390
  • 15
  • 27