title: Android BottomNavigationView ViewModel setting state programmatically
body: <p>I am trying to move my bottom sheet navigation state to a viewmodel.  This will enable me to set the tab programmatically and also save the tab state on rotation.</p>
  <p>In the viewmodel:
  enum class NavTab { TAB_1, TAB_2, TAB_3 }</p>
  <pre><code>val navigationTab: LiveData&lt;NavigationTab&gt; = savedStateHandle.getLiveData(&quot;nav_tab&quot;)
  fun setNavigationTab(tab: NavigationTab) {
      savedStateHandle.set(&quot;nav_tab&quot;, tab)
  }
  </code></pre>
  <p>In the activity:</p>
  <pre><code>bottomNavigationView = findViewById(R.id.bottom_navigation);
  bottomNavigationView.setOnItemSelectedListener(item -&gt; {
      switch (item.getItemId()) {
          case R.id.tab_1:
              viewModel.setNavigationTab(NavTab.TAB_1);
              break;
          case R.id.tab_2:
              viewModel.setNavigationTab(NavTab.TAB_2);
              break;
          case R.id.tab_3:
              viewModel.setNavigationTab(NavTab.TAB_3);
              break;
      }
  
      return true;
  });
  
  viewModel.getNavigationTab().observe(this, this::onNavigationTab);
  private void onNavigationTab(NavTab tab) {
      switch (tab) {
          case TAB_1:
              bottomNavigationView.setSelectedItemId(R.id.tab_1);
              break;
          case TAB_2:
              bottomNavigationView.setSelectedItemId(R.id.tab_2);
              break;
          case TAB_3:
              bottomNavigationView.setSelectedItemId(R.id.tab_3);
              break;
      }
  }
  </code></pre>
  <p>However the problem is when setting the tab programmatically by calling <em>setNavigationTab</em> only the fragment is switching the tab is not property selected. I can call</p>
  <pre><code>bottomNavigationView.setSelectedItemId(R.id.tab_2);
  </code></pre>
  <p>However if I do this, the view model will see the change and fire the observer, causing an infinite loop.</p>
  <p>In the case of programmatically changing the tab I need to switch the fragment and the UI tab.  In case of user clicking the tab, I only need to set the fragment as the tab UI is changed appropriately.</p>