Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

android - Change options menu during runtime - invalidateOptionsMenu()

I am creating a menu where one of the items is used the lock an object. When this item is clicked, the menu should be recreated with a button to unlock the item. I created two menus for this. This is working fine. I read that in Android version >= 11 the onPrepareOptionsMenu is no longer called when displaying the menu and I have to call invalidateOptionsMenu(). So I changed the build target (both in the Manifest and in properties) to 11 and ran the app on an AVD of 4.0.3. The program is still working fine, but I thought it shouldn't anymore, and I should check

if (Build.VERSION.SDK_INT >= 11)
{
  invalidateOptionsMenu();
}

This is my code:

public class MainActivity3 extends Activity{

    boolean locked;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        locked = false;
    }

      @Override
      public boolean onCreateOptionsMenu(Menu menu){
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.changing_menu1, menu);

            return true;
        }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {

            menu.clear();
            MenuInflater inflater = getMenuInflater();

            if (locked) {
                inflater.inflate(R.menu.changing_menu2, menu);
            }
            else {
                inflater.inflate(R.menu.changing_menu1, menu);
            }

        return super.onPrepareOptionsMenu(menu);
        }

        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

          case R.id.Menu1:
          break;

          case R.id.Menu2 :
          break;

          case R.id.Menu3 :
          locked = !locked;
          break;

           }
        return true;
        }
}

So the Menu is still refreshed/recreated in 4.0. Did I misunderstand something about the usage of invalidateOptionsMenu();?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

invalidateOptionsMenu() was added to give us the ability to force onCreateOptionsMenu() to be called again. onPrepareOptionsMenu() is still called every time you call the menu.

What you are trying to achieve above is a good example of when to use invalidateOptionsMenu() but because of backwards compatibility you will need to do both:

if (Build.VERSION.SDK_INT >= 11) {
  invalidateOptionsMenu();
}


@Override
public boolean onCreateOptionsMenu(Menu menu){
    if (Build.VERSION.SDK_INT >= 11) {
        selectMenu(menu);
    }
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (Build.VERSION.SDK_INT < 11) {
        selectMenu(menu);
    }
    return true;
}

private void selectMenu(Menu menu) {
    menu.clear();
    MenuInflater inflater = getMenuInflater();

    if (locked) {
        inflater.inflate(R.menu.changing_menu2, menu);
    }
    else {
        inflater.inflate(R.menu.changing_menu1, menu);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...