Evan Evan - 1 month ago 14
Android Question

menu shows when it shouldn't as I set it false

I've encountered another problem with my app. When the cat dies, I hit the "results screen" (it isn't a new activity but I hide all current elements and show a new text view indicating the death cause), and I want to hide the menu items, but for some reason it's not responding to isHeDead(). Here's the code, I hope someone can help me! Basically the problem seems to be that it doesn't see "return false" inside onCreateOptionsMenu because for some reason isHeDead() isn't working there, even though the method works everywhere else.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(fab_menu, menu);
while (!isHeDead()){
return true;
}
return false;
}

public Boolean isHeDead() {
TextView t = (TextView) findViewById(R.id.textDiedOf);
TextView textName = (TextView) findViewById(R.id.CatsTitleStats);
TextView textAge = (TextView) findViewById(R.id.CatsAgeStat);
TextView textStats1 = (TextView) findViewById(R.id.CatsStats);
TextView textStats2 = (TextView) findViewById(R.id.CatsStats2);
ImageView image = (ImageView) findViewById(R.id.imageViewCat);

if (cat.getAge() >= 20) {
textName.setVisibility(View.GONE);
textAge.setVisibility(View.GONE);
textStats1.setVisibility(View.GONE);
textStats2.setVisibility(View.GONE);
image.setVisibility(View.GONE);
t.setText("Sorry! " + getIntent().getStringExtra(KEY_NAME_EXTRA) + " died of old age.");
return true;
} else if (cat.getAnger() >= 10) {
textName.setVisibility(GONE);
textAge.setVisibility(View.GONE);
textStats1.setVisibility(View.GONE);
textStats2.setVisibility(View.GONE);
image.setVisibility(View.GONE);
t.setText("Sorry! " + getIntent().getStringExtra(KEY_NAME_EXTRA) + " died of madness.");
return true;
} ///etc...
}


User options (not sure if this has anything to do with the menu problem but still).

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

/// FEED ///
case R.id.action_feed:
int qualityRandom = (int) (Math.random() * 10);
if (qualityRandom == 5) {
cat.happy(-2);
cat.healthy(-30);
cat.angry(2);
cat.thirsty(2);

ageStat();
Stats1();
Stats2();

if (isHeDead()) {
break;
} else {
Toast.makeText(CatStatus.this, "Food was in a poor state...", Toast.LENGTH_SHORT).show();
break;
}
} else {
cat.happy(1);
cat.healthy(10);
cat.hungry(-3);
cat.angry(-1);

ageStat();
Stats1();
Stats2();

Toast.makeText(CatStatus.this, "Yummy!", Toast.LENGTH_SHORT).show();
break;
}

/// DRINK ///
case R.id.action_drink: ///etc...

Answer

When you hit results screen, if you are not going to a new activity, onCreateOptionsMenu is not being called!

You need to keep a reference to your menu:

private Menu myMenu;
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(fab_menu, menu);
    this.myMenu = menu;
    while (!isHeDead()){
        return true;
    }
    return false;
}

And then, you can call inside your isHeDead() method like this:

menu.setGroupVisible(R.id.main_menu_group, false);

or:

menu.clear();

It depends on what you want.

Check this answer:

Hide/Show Action Bar Option Menu Item for different fragments