ActionBarSherlock
ActionBarSherlock copied to clipboard
Calling supportInvalidateOptionsMenu in onCreateView
When I call SherlockFragmentActivity.supportInvalidateOptionsMenu from SherlockFragment.onCreateView, the menu items cannot be clicked until one exits the activity (on API 8).
This might be prohibited behavior, but I couldn't find any documentation saying so, so it seems like something should be done about it (either making it possible to call the method without repercussions, or noting somewhere that this is not allowed).
The following code (which lacks some layout files) repros the situation on an API 8 emulator. The background will turn red on e.g. API 17, but not API 8.
public class MainActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_container);
FunFragment frag = new FunFragment();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.add(R.id.frag_container, frag, "foobar");
trans.commit();
}
}
public class FunFragment extends SherlockFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getSherlockActivity().supportInvalidateOptionsMenu();
return new View(getActivity());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(0, 5, 0, "Do magic").setIcon(R.drawable.ic_launcher)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 5:
getView().setBackgroundDrawable(new ColorDrawable(0xFFFF0000));
return true;
default:
return false;
}
}
}