title: Room database: getting SELECT MAX() twice after creating new RecyclerView item
body: <p>I have a RecyclerView list of CardView items that is working properly.  Upon creation of a new CardView that is inserted into the database, I would like to fire a Toast that informs the user that the CardView was successfully added and show the CardView number.  The CardView number is the Id of the CardView item inserted into the database.  The data is saved to the database when the user clicks on a Save button that fires onClickSave().</p>
  
  <p>I set up an @Query in the Dao to get the MAX(cardId):</p>
  
  <pre><code>Dao
  ...
  @Query("SELECT MAX(cardId) FROM cards")
  LiveData&lt;Integer&gt; getMax();
  
  @Insert
  void insertCard(Card card);
  </code></pre>
  
  <p>Problem is that two Toasts are firing.  The first Toast is returning the previously created CardView number <strong>and</strong> then the second Toast is firing and it shows the latest CardView number that was just added.  For example, the Toast will show CardView number 33 and then a second Toast fires that shows the expected CardView number 34 that was just created (I confirm that CardViews 33 and 34 are both in the database and the two highest items, using DB Browser for SQLite software).</p>
  
  <pre><code>AddorUpdateCardActivity
  ...
  private int newMax = -1;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      mViewModel = new ViewModelProvider(this).get(cardViewModel.class);        
  }
  
  public void onClickSave(View v) {
  
      // set card data
      // then insert data in database
      mViewModel.insertCard(card1);     
  
      mViewModel.getMax().observe(this, value -&gt; { newMax = value; Toast.makeText(AddorUpdateCardActivity.this, "card #" + newMax + " was saved to the list", Toast.LENGTH_LONG).show();});
  }
  
  ViewModel
  ...
  
  public cardViewModel(Application application) {
      super(application);
      repository = new cardRepository(application);
      getMax = repository.getMax();
  }
  
  public LiveData&lt;Integer&gt; getMax() {
      return getMax;
  }
  
  public void insertCard(Card card) {
      repository.insertCard(card);
  }
  
  cardRepository
  
  private CardDao cardDao;
  private LiveData&lt;Integer&gt; getMax;
  
  
  public cardRepository(Application application) {
      RoomDatabase db = RoomDatabase.getDatabase(application);
      cardDao = db.cardDao();
  }
  
  public LiveData&lt;Integer&gt; getMax() {
      return cardDao.getMax;  
  }
  
  public void insertCard(Quickcard newcard) {
      AsyncTask.execute(() -&gt; cardDao.insertCard(newcard));
  
  } 
  </code></pre>
  
  <p>What am I missing here?  If the card is inserted properly into the database then why wouldn't the ViewModel observer just return this new CardView number rather than two Toasts? </p>
  
  <p>For reference, I show the previous code I used prior to Room and ViewModel that used a cursor to get the latest and highest inserted Id:</p>
  
  <pre><code>public class SQLiteDB extends SQLiteOpenHelper {
  
      ...
      public int getLastInsertId() {
  
      int index = 0;
      SQLiteDatabase sdb = getReadableDatabase();
      Cursor cursor = sdb.query(
              "sqlite_sequence",
              new String[]{"seq"},
              "name = ?",
              new String[]{TABLE_NAME},
              null,
              null,
              null,
              null
      );
  
      sdb.beginTransaction();
      try {
          if (cursor !=null) { 
              if (cursor.moveToLast()) {                    
                  index = cursor.getInt(cursor.getColumnIndex("seq"));
              }
          }
      ...
      }         
      return index;
  }      
  </code></pre>