I am facing an issue with LiveData and Fragment Lifecycle.
The flow goes like this:
onBackPressed
is called to dismiss the 2nd Fragment. 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.
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.