How do I implement onMenuItemSelected?
Closed this issue · 4 comments
If you create a SettingsActivity with the wizard, it creates:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
if (!super.onMenuItemSelected(featureId, item))
{
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
This isn't available to override when using your control and without it, tapping the back button causes some odd behaviour and eventually a crash due to missing controls.
Is it possible to still allow this to be overridden or a work around?
I tried to create SettingActivity using wizard.
but there was no such method... 🤔
So I cannot know what your code is like.
onMenuItemSelected
is final method in AppCompatActivity.
https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity#onMenuItemSelected(int,%20android.view.MenuItem)
Please try to override onOptionsItemSelected
https://developer.android.com/reference/android/app/Activity#onOptionsItemSelected(android.view.MenuItem)
I found it!
It was necessary to specify "Hierarchical Parent" 😅
Please change as follows.
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onOptionsItemSelected(item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onOptionsItemSelected(item);
}
Awesome, thank you!! 👍