title: Why LiveData observer is being triggered twice for a newly attached observer
body: <p><em>My understanding on <code>LiveData</code> is that, it will trigger observer on the current state change of data, and not a series of history state change of data.</em></p>
  
  <p>Currently, I have a <code>MainFragment</code>, which perform <code>Room</code> write operation, to change <strong>non-trashed data</strong>, to <strong>trashed data</strong>.</p>
  
  <p>I also another <code>TrashFragment</code>, which observes to <strong>trashed data</strong>.</p>
  
  <p>Consider the following scenario.</p>
  
  <ol>
  <li>There are currently 0 <strong>trashed data</strong>.</li>
  <li><code>MainFragment</code> is the current active fragment. <code>TrashFragment</code> is not created yet.</li>
  <li><code>MainFragment</code> added 1 <strong>trashed data</strong>.</li>
  <li>Now, there are 1 <strong>trashed data</strong></li>
  <li>We use navigation drawer, to replace <code>MainFragment</code> with <code>TrashFragment</code>.</li>
  <li><code>TrashFragment</code>'s observer will first receive <code>onChanged</code>, with 0 <strong>trashed data</strong></li>
  <li>Again, <code>TrashFragment</code>'s observer will secondly receive <code>onChanged</code>, with 1 <strong>trashed data</strong> </li>
  </ol>
  
  <p>What is out of my expectation is that, item (6) shouldn't happen. <code>TrashFragment</code> should only receive latest <strong>trashed data</strong>, which is 1.</p>
  
  <p>Here's my codes</p>
  
  <hr>
  
  <h2>TrashFragment.java</h2>
  
  <pre><code>public class TrashFragment extends Fragment {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          noteViewModel = ViewModelProviders.of(getActivity()).get(NoteViewModel.class);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          ...
  
          noteViewModel.getTrashedNotesLiveData().removeObservers(this);
          noteViewModel.getTrashedNotesLiveData().observe(this, notesObserver);
  </code></pre>
  
  <h2>MainFragment.java</h2>
  
  <pre><code>public class MainFragment extends Fragment {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          noteViewModel = ViewModelProviders.of(getActivity()).get(NoteViewModel.class);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          ...
  
          noteViewModel.getNotesLiveData().removeObservers(this);
          noteViewModel.getNotesLiveData().observe(this, notesObserver);
  </code></pre>
  
  <h2>NoteViewModel .java</h2>
  
  <pre><code>public class NoteViewModel extends ViewModel {
      private final LiveData&lt;List&lt;Note&gt;&gt; notesLiveData;
      private final LiveData&lt;List&lt;Note&gt;&gt; trashedNotesLiveData;
  
      public LiveData&lt;List&lt;Note&gt;&gt; getNotesLiveData() {
          return notesLiveData;
      }
  
      public LiveData&lt;List&lt;Note&gt;&gt; getTrashedNotesLiveData() {
          return trashedNotesLiveData;
      }
  
      public NoteViewModel() {
          notesLiveData = NoteplusRoomDatabase.instance().noteDao().getNotes();
          trashedNotesLiveData = NoteplusRoomDatabase.instance().noteDao().getTrashedNotes();
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>Code which deals with Room</h2>
  
  <pre><code>public enum NoteRepository {
      INSTANCE;
  
      public LiveData&lt;List&lt;Note&gt;&gt; getTrashedNotes() {
          NoteDao noteDao = NoteplusRoomDatabase.instance().noteDao();
          return noteDao.getTrashedNotes();
      }
  
      public LiveData&lt;List&lt;Note&gt;&gt; getNotes() {
          NoteDao noteDao = NoteplusRoomDatabase.instance().noteDao();
          return noteDao.getNotes();
      }
  }
  
  @Dao
  public abstract class NoteDao {
      @Transaction
      @Query("SELECT * FROM note where trashed = 0")
      public abstract LiveData&lt;List&lt;Note&gt;&gt; getNotes();
  
      @Transaction
      @Query("SELECT * FROM note where trashed = 1")
      public abstract LiveData&lt;List&lt;Note&gt;&gt; getTrashedNotes();
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      public abstract long insert(Note note);
  }
  
  @Database(
          entities = {Note.class},
          version = 1
  )
  public abstract class NoteplusRoomDatabase extends RoomDatabase {
      private volatile static NoteplusRoomDatabase INSTANCE;
  
      private static final String NAME = "noteplus";
  
      public abstract NoteDao noteDao();
  
      public static NoteplusRoomDatabase instance() {
          if (INSTANCE == null) {
              synchronized (NoteplusRoomDatabase.class) {
                  if (INSTANCE == null) {
                      INSTANCE = Room.databaseBuilder(
                              NoteplusApplication.instance(),
                              NoteplusRoomDatabase.class,
                              NAME
                      ).build();
                  }
              }
          }
  
          return INSTANCE;
      }
  }
  </code></pre>
  
  <p>Any idea how I can prevent from receiving <code>onChanged</code> twice, for a same data?</p>
  
  <hr>
  
  <h2>Demo</h2>
  
  <p>I created a demo project to demonstrate this problem.</p>
  
  <p>As you can see, after I perform write operation (Click on <strong>ADD TRASHED NOTE</strong> button) in <code>MainFragment</code>, when I switch to <code>TrashFragment</code>, I expect <code>onChanged</code> in <code>TrashFragment</code> will only be called once. However, it is being called twice.</p>
  
  <p><a href="https://i.stack.imgur.com/uLsLE.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/uLsLE.gif" alt="enter image description here"></a></p>
  
  <p>Demo project can be downloaded from <a href="https://github.com/yccheok/live-data-problem" rel="noreferrer">https://github.com/yccheok/live-data-problem</a></p>