使用设备api级别14(SherlockActionBar)时未调用onOptionsItemSelected

时间:2012-10-19 08:03:07

标签: android actionbarsherlock android-4.0-ice-cream-sandwich

这是SherlockActionBar示例,它是使用静态附件,我运行到ICE_CREAM_SANDWICH设备,但没有调用方法onOptionsItemSelected

删除了部分代码,3.x和2.x版本没有问题,帮帮我~~

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(SampleList.THEME); // Used for theme switching in samples
    super.onCreate(savedInstanceState);

    mSherlock
            .setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
    mSherlock.setContentView(R.layout.text);

    ((TextView) findViewById(R.id.text))
            .setText(R.string.static_attach_content);
}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    return mSherlock.dispatchCreateOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Used to put dark icons on light action bar
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;

    menu.add("Save")...

    menu.add("Search")...

    menu.add("Refresh")...

    return true;
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mSherlock.dispatchKeyEvent(event)) {
        return true;
    }
    return super.dispatchKeyEvent(event);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i("", "on options item selected!!!!!!!!!!!!!!!!");
    Toast.makeText(this, item.toString(), Toast.LENGTH_SHORT).show();
    return true;
}

}

1 个答案:

答案 0 :(得分:3)

如果您正在使用片段并在片段和活动中调用onOptionsItemSelected,请记得在活动调用中返回super.onOptionsItemSelected(item);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // If home icon is clicked return to main Activity
    case android.R.id.home:
        getSupportFragmentManager().popBackStack(null,
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
        return true;
    }

    return super.onOptionsItemSelected(item);
}