-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
pieeet
|
||
} else { | ||
// Otherwise this is an existing pet, so change app bar to say "Edit Pet" | ||
setTitle(getString(R.string.editor_activity_title_edit_pet)); | ||
|
@@ -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 | ||
|
13 comments
on commit d90609a
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its fine
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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.
what this method really do ?! because onPrepareOptionsMenu done the job without this method been used !