title: Menu items won't get destroyed on rotation - kotlin
body: <p>I've created simple app using two fragments. One is responsible for displaying the scrollview of thumbnails. The other one is displayed only in landscape mode or in other activity after clicking on one of the textviews in first fragment and is showing details of clicked textview. The second fragment also has &quot;share&quot; menu item.</p>
  <p><a href="https://i.stack.imgur.com/SoFqI.png" rel="nofollow noreferrer">How the portrait layout should look like</a></p>
  <p>When rotating phone to landscape mode the &quot;share&quot; menu item of the second fragment is displayed correctly in the menu. When rotating the phone back to portrait, the &quot;share&quot; button is still visible even when it should be destroyed.</p>
  <p><a href="https://i.stack.imgur.com/qQeky.png" rel="nofollow noreferrer">Landscape mode</a></p>
  <p>Here the menu should be empty again:</p>
  <p><a href="https://i.stack.imgur.com/OulKh.png" rel="nofollow noreferrer">The wrong menu layout - how it actually looks like</a></p>
  <p>This is how I'm creating share button in second fragment</p>
  <pre><code>override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
          // Also tried menu.clear(), that didn't work either
          if (menu.findItem(R.menu.detail_menu ) == null){
              inflater.inflate(R.menu.detail_menu, menu)
          }
          super.onCreateOptionsMenu(menu, inflater);
      }
  
      override fun onOptionsItemSelected(item: MenuItem): Boolean {
          if (item.itemId == R.id.item_menu_share){
              Log.i(TAG, &quot;Clicked on share&quot;)
              val i = Intent(Intent.ACTION_SEND)
              i.type = &quot;text/plain&quot;
              i.putExtra(Intent.EXTRA_SUBJECT, &quot;Sharing URL&quot;)
              i.putExtra(Intent.EXTRA_TEXT, DataProvider.data[identifier]?.address)
              startActivity(Intent.createChooser(i, &quot;Share URL&quot;))
          }else{
              return super.onOptionsItemSelected(item)
          }
          return true
      }
  </code></pre>
  <p>How do I remove the element from the menu or how do I stop it from being shown even after rotation? I've</p>