ohmae/preference-activity-compat

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?

ohmae commented

I tried to create SettingActivity using wizard.
2018-12-21
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)

ohmae commented

I found it!
It was necessary to specify "Hierarchical Parent" 😅
2018-12-21 1

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);
    }
ohmae commented

I updated readme 7f241ed

Awesome, thank you!! 👍