Search code examples
androidlifecycleobservers

fragment observe issue


I am facing an issue with LiveData and Fragment Lifecycle.

The flow goes like this:

  • The 1st Fragment shows a recyclerview of comments. Each comment has a delete button.
  • The 1st Fragment has a menu button, which when pressed, navigates the user to a 2nd Fragment to create a new comment.
  • When the user creates the comment, a feedback dialog is shown and onBackPressed is called to dismiss the 2nd Fragment.
  • The 1st Fragment refreshes the list of comments to show the changes.

Take a look at the code snippets below. The method observe is getting called on the Fragment's onCreateView.

1st Fragment:

private void observe() {
    //getComments method observes the API response from the allComments method
    mainViewModel.getComments().observe(getViewLifecycleOwner(), resp -> {
        if (resp != null && resp.getStatus().equalsIgnoreCase("success")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage("success message");
            builder.setPositiveButton("ΟΚ", (dialog, id) -> {
                dialog.dismiss();
                getActivity().onBackPressed();
            });
            builder.create().show();
        }
    });
}
mainViewModel.getCommentDelete().observe(getViewLifecycleOwner(), resp -> {
        if (resp != null && resp.getStatus().equalsIgnoreCase("success")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage("Comment deleted.");
            builder.setPositiveButton("ΟΚ", (dialog, id) -> {
                dialog.dismiss();
            });
            builder.create().show();
            //allComments method makes an http call to the API to fetch comments
            mainViewModel.allComments(mainViewModel.getSelected().getId());
        }
    });

//initView. The adapter has a listener to listen for button clicks.
mAdapter = new BuildingCommentsAdapter(new RecyclerMultipleOptionsClick() {
        @Override
        public void onItemClicked(int position, int id) {
            Comments comment = mAdapter.getItemAtPosition(position);
            switch (id) {
                case R.id.comment_save:
                    mainViewModel.commentUpdate(new UpdateCommentReq(mainViewModel.getSelected().getId(), comment.getComment(), comment.getId()));
                    break;
                case R.id.comment_delete:
                    mainViewModel.commentDelete(comment.getId());
                    break;
            }

        }
    });

The issue i am facing is: I run the app and I go to the comment list fragment, I press the add comment button and go to 2nd Fragment. Adding a comment and going back. So far so good. Now, I delete one comment from the list (works great). I navigate again to the 2nd Fragment to create a new comment and going back again to the 1st Fragment. The mainViewModel.getCommentDelete().observe(..) runs again, showing the success dialog (without deleting something). The problem is that the observer is triggered again and again once I delete one comment from the list.

I can provide more code if you like. Any help would be really helpful.


Solution

  • Problem is LiveData cached your value, and when you comeback to screen you are getting the old value. Should google about SingleLiveData event for navigation purpose. Here or here.