title: How to handle clicking back arrow in SearchView in Toolbar
body: <p>How to handle clicking back arrow <a href="https://i.stack.imgur.com/7QK2g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7QK2g.png" alt="enter image description here" /></a> in <code>androidx.appcompat.widget.SearchView</code>?</p>
  <p><a href="https://i.stack.imgur.com/MB7Qr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MB7Qr.png" alt="enter image description here" /></a></p>
  <p>I founded this answer: <a href="https://stackoverflow.com/questions/45635482/how-to-handle-back-arrow-event-in-a-searchview">How to handle back arrow event in a SearchView</a> but it isn't working for me. I set the menu from a fragment.</p>
  <p>In MainActivity I have this functions:</p>
  <pre><code>override fun onOptionsItemSelected(item: MenuItem): Boolean
  {
      Log.d(&quot;MyTag&quot;, &quot;Activity&quot;)
      return super.onOptionsItemSelected(item)
  }
  
  override fun onSupportNavigateUp(): Boolean
  {
      Log.d(&quot;MyTag&quot;, &quot;Up&quot;)
      return navController.navigateUp()
  }
  </code></pre>
  <p>In Fragment:</p>
  <pre><code>searchView.setOnQueryTextListener(
      object : SearchView.OnQueryTextListener
      {
          override fun onQueryTextSubmit(p0: String?): Boolean
          {
              Log.d(&quot;MyTag&quot;, &quot;onQueryTextSubmit&quot;)
              hideKeyboard()
              return true
          }
          override fun onQueryTextChange(p0: String?): Boolean
          {
              return true
          }
      }
  )
  
  override fun onOptionsItemSelected(item: MenuItem): Boolean
  {
      Log.d(&quot;MyTag&quot;, &quot;Fragment&quot;)
      return super.onOptionsItemSelected(item)
  }
  </code></pre>
  <p>None of these functions are called when I click the back arrow. Logcat looks like this:</p>
  <pre><code>// clicking search button
  Activity  
  Fragment
  
  // clicking Enter on keyboard
  onQueryTextSubmit
  
  // clicking back arrow (Nothing)
  
  // When I go to the next fragment and navigate back
  Activity
  Up
  </code></pre>
  <p>Why there isn't called any event when I click the back arrow in SearchView? How I can achieve this?</p>
  <hr>
  <p>Thanks to <a href="https://stackoverflow.com/users/1791771/rustam-samandarov">Rustam Samandarov</a> here is a full code of <code>onCreateOptionsMenu</code>:</p>
  <pre><code>override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
  {
      super.onCreateOptionsMenu(menu, inflater)
      inflater.inflate(R.menu.menu_toolbar_home, menu)
  
      val menuItem = menu.findItem(R.id.itemSearch)
      val searchView = menuItem.actionView as SearchView
  
      searchView.setOnQueryTextListener(
          object : SearchView.OnQueryTextListener
          {
              override fun onQueryTextSubmit(p0: String?): Boolean
              {
                  Log.d(&quot;MyTag&quot;, &quot;onQueryTextSubmit&quot;) // called when enter is pressed
                  hideKeyboard()
                  return true
              }
              override fun onQueryTextChange(p0: String?): Boolean
              {
                  return true // called every time when text is changed
              }
          }
      )
  
      menuItem.setOnActionExpandListener(
          object : MenuItem.OnActionExpandListener
          {
              override fun onMenuItemActionExpand(p0: MenuItem?): Boolean
              {
                  Log.d(&quot;MyTag&quot;, &quot;onMenuItemActionExpand&quot;) // called when user opens SearchView
                  return true;
              }
              override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean
              {
                  Log.d(&quot;MyTag&quot;, &quot;onMenuItemActionCollapse&quot;) // called when user click back arrow or navigation back button
                  return true
              }
          }
      )
  }
  </code></pre>