EDIT: I managed to do what I want in a next way:
STEP 1. I change ViewModel from POJO to Arraylist with POJO - ArrayList:
public class TransactionViewModel extends ViewModel {
private final MutableLiveData<ArrayList<TransactionItem>> selected = new MutableLiveData<>();
public void setSelected (ArrayList<TransactionItem> arrayList){
selected.setValue(arrayList);
}
public LiveData<ArrayList<TransactionItem>> getSelected() {
return selected;
}
}
STEP 2. In Fragment A I added ArrayList with the same POJO type, in onActivityResult. I changed the code now object will be created and added after Activity will return a result, not in Fragment B:
public class Fragment A extends Fragment {
ArrayList<TransactionItem> listTransactions = new ArrayList<>();
…
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode != RESULT_CANCELED) {
...
//Create TransactionItem and use setSelected method from ViewModel
TransactionItem item = new TransactionItem(accountTransaction,
String.valueOf(getDouble),categoryAccount),transactionID);
listTransactions.add(0,item);
model.setSelected(listTransactions);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Must notice that I added transactionID into the TransactionItem constructor, and that's why we need it.
STEP 3 I created next TransactionDiffUtilCallback class that extends DiffUtil.Callback :
public class TransactionDiffUtilCallback extends `DiffUtil.Callback` {
public TransactionDiffUtilCallback(ArrayList<TransactionItem> oldList, ArrayList<TransactionItem> newList) {
this.oldList = oldList;
this.newList = newList;
}
ArrayList<TransactionItem> newList;
@Override
public int getOldListSize() {
return oldList.size();
}
@Override
public int getNewListSize() {
return newList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).getItemIID() == newList.get(newItemPosition).getItemIID();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
return super.getChangePayload(oldItemPosition, newItemPosition);
}
}
I used getItemIID()
from POJO to notify that the new item in ArrayList is different.
STEP 4 In recyclerview adapter I created updateItemList(list):
public void updateItemList(ArrayList<TransactionItem> items){
final TransactionDiffUtilCallback diffCallback = new TransactionDiffUtilCallback(this.pad_list, items);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
this.pad_list.clear();
this.pad_list.addAll(items);
diffResult.dispatchUpdatesTo(this);
}
So this method uses DiffUtil.CallBack to compare items in ArrayList from Fragment A and ArrayList in Fragment B, then notify adapter that ArrayList from Fragment A is different, and this data should be put in ArrayList in Fragment B, and view should be updated.
STEP 5 In Fragment B OnViewCreated() code was rewritten to observe Arraylist forever :
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
initRecView();
model = new ViewModelProvider(requireActivity()).get(TransactionViewModel.class);
Observer<ArrayList<TransactionItem>> observer = (Observer<ArrayList<TransactionItem>>) this::initObserve;
model.getSelected().observeForever(observer);
super.onViewCreated(view, savedInstanceState);
}
And initObserve() now have next code:
private void initObserve(ArrayList<TransactionItem> list){
adapter.updateItemList(list);
}
For now, this solution is working, the user doesn’t need to switch to Fragment B to keep transaction recording. I will resume test this solution.