Skip to content

Commit

Permalink
1.26 Hide delete menu option for new pets
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent bea7d90 commit d90609a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/android/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ protected void onCreate(Bundle savedInstanceState) {
if (mCurrentPetUri == null) {
// This is a new pet, so change the app bar to say "Add a Pet"
setTitle(getString(R.string.editor_activity_title_new_pet));

// Invalidate the options menu, so the "Delete" menu option can be hidden.
// (It doesn't make sense to delete a pet that hasn't been created yet.)
invalidateOptionsMenu();

This comment has been minimized.

Copy link
@Muhammad91

Muhammad91 Feb 19, 2017

what this method really do ?! because onPrepareOptionsMenu done the job without this method been used !

This comment has been minimized.

Copy link
@pieeet

pieeet Feb 24, 2017

this method invokes the onPrepareOptionsMenu being called (again) so you can dynamically (at runtime) change menu items. It's not a very good example since the onPrepareOptions method is being called after onCreateOptionsMenu method anyways.

This comment has been minimized.

Copy link
@typebrook

typebrook Feb 26, 2017

@pieeet I got it. So when we call invalidateOptionsMenu(), the menu display again. So onPrepareOptionMenu() will be invoked. But when is it invoked before runtime? After onCreateOptionsMenu()?

This comment has been minimized.

Copy link
@pieeet

pieeet Feb 26, 2017

@typebrook If you make a log message in both methods you'll see that onCreateOptionsMenu is being called first.

This comment has been minimized.

Copy link
@typebrook

typebrook Feb 26, 2017

Log method! Why I didn't try that? Thanks for your reply.

} else {
// Otherwise this is an existing pet, so change app bar to say "Edit Pet"
setTitle(getString(R.string.editor_activity_title_edit_pet));
Expand Down Expand Up @@ -244,6 +248,21 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

/**
* This method is called after invalidateOptionsMenu(), so that the
* menu can be updated (some menu items can be hidden or made visible).
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// If this is a new pet, hide the "Delete" menu item.
if (mCurrentPetUri == null) {
MenuItem menuItem = menu.findItem(R.id.action_delete);
menuItem.setVisible(false);
}
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
Expand Down

13 comments on commit d90609a

@siralam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I don't get it, in this case, we don't we simply set the if and else inside onCreateOptionsMenu?
I have tried and it works indeed.

@PackHg
Copy link

@PackHg PackHg commented on d90609a May 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsw19921030, onCreateOptionsMenu() should be only used to create the initial menu state and not to make changes during the activity lifecycle.
Android - Correct use of invalidateOptionsMenu()

@karkinissan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PackHg Since we are modifying the menu items on the activity launch itself, instead of a button press or something other, cant we just do this all in onCreateOptionsMenu()?

@Bell-Joe
Copy link

@Bell-Joe Bell-Joe commented on d90609a Sep 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karkinissan I agree, don't see why we need to make the change during the activity lifecycle unless there's something I'm missing. For instance:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu options from the res/menu/menu_editor.xml file.

        // This adds menu items to the app bar.

        getMenuInflater().inflate(R.menu.menu_editor, menu);

        MenuItem deleteItem = menu.findItem(R.id.action_delete);

        deleteItem.setVisible(mEditPet);

        return true;
    }

(mEditPet is true if when editing a pet rather than making a new one).

@n-abdelmaksoud
Copy link

@n-abdelmaksoud n-abdelmaksoud commented on d90609a Nov 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the method onPrepareOptionsMenu() is called just before onCreateOptionsMenu() is called, and you can re-call onPrepareOptionsMenu() by invoking invalidateOptionsMenu() so here the delete action menu item is invisible in all cases.
from documentation:

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown.

so i think we should do the check inside onPrepareOptionsMenu()

 @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if(currentMode==Mode.SAVE_MODE){
            MenuItem menuItem=menu.findItem(R.id.action_delete);
            menuItem.setVisible(false);
            return true;
        }
       return super.onPrepareOptionsMenu(menu);
    }

i used enum mode declared inside EditorActivity to set its mode: save new Pet or Edit Pet

 private enum Mode { EDIT_MODE, SAVE_MODE}
    Mode currentMode;

@alexejmamaev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nerrydanna Using enums in android apps is not recommended because of extensive RAM usage.
https://android.jlelse.eu/android-performance-avoid-using-enum-on-android-326be0794dc3

@Babadzhanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its fine

@Nikoloutsos
Copy link

@Nikoloutsos Nikoloutsos commented on d90609a Jun 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a bug : When we click on a pet and change for example it's gender and we rotate the phone then our app doesn't show any Alert Dialog (Discard/Keep editing) when we hit the back button.
I guess this happens because when we rotate the phone the activity is killed and another one is created which has lost the previous mPetHasChanged boolean value.
With that said, how can we overcome this issue?

@siralam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arkoydas Try to google "Android onSavedInstanceState".

@harshabhadra
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nikoloutsos it's not a bug , it's not showing the alert dialog when you rotate the screen because in code we specify that the dialog will be shown only when we click the back or up button.

@Barranha
Copy link

@Barranha Barranha commented on d90609a Jun 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a bug : When we click on a pet and change for example it's gender and we rotate the phone then our app doesn't show any Alert Dialog (Discard/Keep editing) when we hit the back button.
I guess this happens because when we rotate the phone the activity is killed and another one is created which has lost the previous mPetHasChanged boolean value.
With that said, how can we overcome this issue?

in OnCreate() method put this:

        if (savedInstanceState != null) {
            mPetHasChanged = savedInstanceState.getBoolean("mPetHasChanged");
        }

Then, Override the method onSaveInstanceState():

@Override
public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("mPetHasChanged", mPetHasChanged);
    }


@android-coders-rc
Copy link

@android-coders-rc android-coders-rc commented on d90609a Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Android Studio 4.0 and above,
Use the following in place of onPrepareOptionsMenu

@OverRide
public boolean onPreparePanel(int featureId, @nullable View view, @nonnull Menu menu) {
super.onPreparePanel(featureId, view, menu);

    if(mCurrentPetUri == null)
    {
        MenuItem menuItem = menu.findItem(R.id.action_delete);
        menuItem.setVisible(false);
    }
    return  true;
}

@rahulkr25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a bug : When we click on a pet and change for example it's gender and we rotate the phone then our app doesn't show any Alert Dialog (Discard/Keep editing) when we hit the back button.
I guess this happens because when we rotate the phone the activity is killed and another one is created which has lost the previous mPetHasChanged boolean value.
With that said, how can we overcome this issue?

in OnCreate() method put this:

        if (savedInstanceState != null) {
            mPetHasChanged = savedInstanceState.getBoolean("mPetHasChanged");
        }

Then, Override the method onSaveInstanceState():

@Override
public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("mPetHasChanged", mPetHasChanged);
    }

It helped.

Please sign in to comment.