0

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().

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...
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

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

Community
  • 1
  • 1
caiolopes
  • 561
  • 8
  • 14
  • Thanks man! works now. Is there a way to make this hiding action quicker though? because right now it shows how it moves left and disappears, and I'd prefer it if this happened so quick that the user wouldn't even notice. Is this possible or would I need a new activity? –  Nov 02 '16 at 20:38
  • In my opinion, I would go with fragments! You could create only one activity, and then two fragments, and then you would make a transaction between the two fragments when the user clicks to see the results. It is a cleaner solution than hiding elements to make a new screen for the user. Or you could go with two activities too, it is fine. But it is good to have a separation of screens in different fragments/activities. – caiolopes Nov 02 '16 at 20:56
  • Both. And both give me the same moving-left-then-disappears effect. –  Nov 02 '16 at 20:58
  • Thanks! I don't know what fragments are, never used them before, but will look into it! –  Nov 02 '16 at 21:00
0

onCreateOptionsMenu is called only one time when the app launch activity

giuseppe trubia
  • 142
  • 1
  • 13
  • thanks! fixed it and it works now although not at the speed I'd like! –  Nov 02 '16 at 20:39