Change menu item after selection in Android
<p>I am trying to replace the menu item to another menu item after selection. I tried the following but it is not working as expected. Any solutions to this. thanks.</p>

<p>The menu.xml file is:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;
    &lt;item android:id="@+id/add" android:visible="true" android:enabled="true" android:title="Add"&gt;&lt;/item&gt;
    &lt;item android:id="@+id/delete" android:visible="false" android:enabled="true" android:title="Delete"&gt;&lt;/item&gt;
&lt;/menu&gt;
</code></pre>

<p>The code inside MyActivity.java is:</p>

<pre><code>@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.defaultmenu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        if(isAdded) {
            menu.removeItem(R.id.add);
            menu.add(0, R.id.delete, 0, "Delete");
        } else {
            menu.removeItem(R.id.delete);
            menu.add(0, R.id.add, 0, "Add");
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.add:
                isLogin = true;
                return true;
            case R.id.delete:
                isLogin = false;
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
</code></pre>
