Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same problem, but I think it's better to summarize and introduce the last step to get it working:</p> <ol> <li><p>Add <code>setHasOptionsMenu(true)</code> method in your Fragment's <code>onCreate(Bundle savedInstanceState)</code> method.</p></li> <li><p>Override <code>onCreateOptionsMenu(Menu menu, MenuInflater inflater)</code> (if you want to do something different in your Fragment's menu) and <code>onOptionsItemSelected(MenuItem item)</code> methods in your Fragment.</p></li> <li><p>Inside your Activity's <code>onOptionsItemSelected(MenuItem item)</code> method, make sure you return <code>false</code> when the menu item action would be implemented in Fragment's <code>onOptionsItemSelected(MenuItem item)</code> method.</p></li> </ol> <p>An example:</p> <h1>Activity</h1> <pre><code>@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.activity_menu_item: // Do Activity menu item stuff here return true; case R.id.fragment_menu_item: // Not implemented here return false; default: break; } return false; } </code></pre> <h1>Fragment</h1> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); .... } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // Do something that differs the Activity's menu here super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.activity_menu_item: // Not implemented here return false; case R.id.fragment_menu_item: // Do Fragment menu item stuff here return true; default: break; } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload