Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - Android: onOptionsItemSelected not called

I'm working on a code that was pre-written by another company. Now I'm trying to add a hamburger menu icon on the top left on the action bar. The way I've done that is:

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if(!(getActivity() instanceof AppCompatActivity))
        return;
    AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
    appCompatActivity.getSupportActionBar().setHomeButtonEnabled(true);
    Drawable hamburger = getResources().getDrawable(R.drawable.mc_tab_icon_sections);
    hamburger.setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP);
    appCompatActivity.getSupportActionBar().setHomeAsUpIndicator(hamburger);// set drawable icon

    appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

in my fragment and that works perfectly fine.

But now when a user might tap on this icon, this is how I'm handling it:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            showSectionsMenu();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

and the way the action bar is created in the activity is:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mc_main, menu);
    if (menu != null) {
        search = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) search.getActionView();
        toolbarStateModel.setSearchView(searchView);
        search.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return false;
            }
        });

        settingsActionItem = menu.findItem(R.id.action_settings);
        settingsActionItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                controller.getNavigation().openSettings(BaseMainActivity.this);
                return false;
            }
        });
    }

    return true;
}

But now when I tap on the hamburger menu icon, instead of it calling "onOptionsItemSelected". It just closes the app and nowhere in the app "onOptionsItemSelected" is called.

Any help will be highly appreciated. TIA


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...