Note that there are some explanatory texts on larger screens.

plurals
  1. POMenu Ordering differences due to Fragment and Activity hierarchy. (onCreateOptionsMenu)
    primarykey
    data
    text
    <p><strong>Background</strong></p> <p>Developing for Android 3.0, I have a <code>HostActivity</code> that is the superclass of <code>NotebooksActivity</code> and <code>NoteActivity</code>. <code>NotebooksActivity</code> includes a fragment, <code>NotebooksFragment</code>.</p> <p>In <code>HostActivity</code>, I include a menu that I want to appear at the rightmost end of the options menu in the ActionBar, <strong>i.e. all menu items in subclasses of <code>HostActivity</code> should appear to the left of the menu items added in <code>HostActivity</code>.</strong></p> <p>Menu inflation in <code>HostActivity</code>:</p> <pre><code>@Override public boolean onCreateOptionsMenu(Menu menu) { final MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.host_menu, menu); return super.onCreateOptionsMenu(menu); } </code></pre> <p><strong>The Problem</strong></p> <p>When I add menu items in <code>NoteActivity</code>, I achieve the desired order as expected: </p> <pre><code>@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.notebook_menu, menu); return super.onCreateOptionsMenu(menu); } </code></pre> <p>However, when I add menu items in <code>NotebooksFragment</code>, because of how Fragments work, <code>onCreateOptionsMenu</code> is called after the same method in <code>HostActivity</code>, <strong>resulting in <code>HostActivity</code>'s menu items appearing before <code>NotebooksFragment</code>'s.</strong></p> <pre><code>@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.notebooks_menu, menu); SearchView sv = new SearchView(getActivity()); sv.setOnQueryTextListener(this); menu.findItem(R.id.search_notebooks).setActionView(sv); super.onCreateOptionsMenu(menu, inflater); } </code></pre> <p>How can I achieve my desired menu ordering?</p>
    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.
 

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