Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Following will make things more clear:</p> <p><strong>1. onCreateOptionsMenu()</strong></p> <p>To specify the options menu for an activity,you override <code>onCreateOptionsMenu()</code>. In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For <code>Android 2.3.x and lower</code>, the system calls <code>onCreateOptionsMenu()</code> to create the options menu when the user opens the menu for the first time. However for <code>Android 3.0 and higher</code>, the system calls <code>onCreateOptionsMenu()</code> when starting the activity, in order to show items to the action bar.</p> <p>After the system calls <code>onCreateOptionsMenu()</code>, it retains an instance of the Menu you populate and will not call <code>onCreateOptionsMenu()</code> again unless the menu is invalidated for some reason. However, you should use <code>onCreateOptionsMenu()</code> only to create the initial menu state and not to make changes during the activity lifecycle.</p> <p><strong>2. onOptionsItemSelected()</strong></p> <p>When the you select an item from the options menu the system calls your activity's <code>onOptionsItemSelected()</code> method. This method passes the MenuItem selected.</p> <p><strong>3. onPrepareOptionsMenu()</strong></p> <p>If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the <code>onPrepareOptionsMenu()</code> method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items.</p> <p>On <code>Android 2.3.x and lower</code>, the system calls <code>onPrepareOptionsMenu()</code> each time the user opens the options menu (presses the Menu button).</p> <p>On <code>Android 3.0 and higher</code>, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call <code>invalidateOptionsMenu()</code> to request that the system call <code>onPrepareOptionsMenu()</code>.</p> <p>For <code>invalidateOptionsMenu()</code> <a href="http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()" rel="nofollow noreferrer">Refer this</a></p> <p>For <code>onPrepareOptionsMenu()</code> <a href="http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)" rel="nofollow noreferrer">Refer this</a></p> <p><strong>Hence you can try as follows:</strong></p> <p>In order to deal with backward compatibility add following in your activity.</p> <pre><code> @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.Menu1: refreshMenu(); break; case R.id.Menu2: refreshMenu(); break; } return super.onOptionsItemSelected(item); } public void refreshMenu() { if (Build.VERSION.SDK_INT &gt;= 11) { invalidateOptionsMenu(); } } </code></pre> <p>You can also <a href="https://stackoverflow.com/a/7814662/1554935">refer this</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. 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