title: MutableLiveData containing List<> always passed by reference. Want to pass it by value
body: <p>I have a seperate class for my sqlite operations named <code>DBOperations.java</code> which fetch data and pass all data to my ViewModel named <code>HomeActivityViewModel.java</code> through a interface named DBOperationsListener</p>
  <pre><code>public interface DBOperationsListener {
      void itemDataLoaded(List&lt;ItemData&gt; itemDataList);
  }
  </code></pre>
  <p>my <code>HomeActivityViewModel.java</code></p>
  <pre><code>public class HomeActivityViewModel extends AndroidViewModel implements DBOperations.DBOperationsListener {
  
      private DBOperations dbOperations;
      private MutableLiveData&lt;List&lt;ItemData&gt;&gt; orderItemDataMLData = new MutableLiveData&lt;&gt;();
      private MutableLiveData&lt;List&lt;ItemData&gt;&gt; returnItemDataMLData = new MutableLiveData&lt;&gt;();
      
      public HomeActivityViewModel(@NonNull Application application) {
          super(application);
          this.application = application;
          dbOperations = new DBOperations(application, this);
          dbOperations.loadItemData();
      }
  
      public MutableLiveData&lt;List&lt;ItemData&gt;&gt; getOrderItemDataMLData() {
          return orderItemDataMLData;
      }
  
      public MutableLiveData&lt;List&lt;ItemData&gt;&gt; getReturnItemDataMLData() {
          return returnItemDataMLData;
      }
  
      @Override
      public void itemDataLoaded(List&lt;ItemData&gt; itemDataList) {
          orderItemDataMLData.postValue(new ArrayList&lt;&gt;(itemDataList));
          returnItemDataMLData.postValue(new ArrayList&lt;&gt;(itemDataList));
      }
  </code></pre>
  <p>on my <code>FirstFragment</code></p>
  <pre><code>private List&lt;ItemData&gt; itemDataList;
  homeActivityViewModel.getOrderItemDataMLData().observe(getViewLifecycleOwner(), (itemData) -&gt; {
      itemDataList = itemData;
  });
  </code></pre>
  <p>then later on the same fragment I get an item and update the values of that item as this</p>
  <pre><code>ItemData item = itemDataList.get(index);
  item.setValue(value);
  </code></pre>
  <p>When I update that value both MutableLiveData lists (<code>orderItemDataMLData</code> and <code>returnItemDataMLData</code>) on the <code>HomeActivityViewModel</code> item value is set to the value that has assigned on the fragment.</p>
  <p>What I have Tried : changed method <code>itemDataLoaded</code> to</p>
  <pre><code>@Override
  public void itemDataLoaded(List&lt;ItemData&gt; itemDataList) {
      List&lt;ItemData&gt; itemDataArrayList1 = new ArrayList&lt;&gt;();
      itemDataArrayList1.addAll(itemDataList);
      orderItemDataMLData.postValue(itemDataArrayList1);
      List&lt;ItemData&gt; itemDataArrayList2 = new ArrayList&lt;&gt;();
      itemDataArrayList2.addAll(itemDataList);
      returnItemDataMLData.postValue(itemDataArrayList2);
  }
  </code></pre>
  <p>but still both MutableLiveData is updated when the value assigned.</p>
  <p>I cannot understand why it is happening. since I have created two new ArrayLists before assigning the value.</p>
  <p>please follow <a href="https://github.com/connorT6/ViewModelError" rel="nofollow noreferrer">this link</a> to the example app which creates this problem</p>