Fragment not receiving menu callbacks

2019-01-17 05:52发布

I have a fragment class that extends Fragment and calls setHasOptionsMenu to participate in the menu. This class also implements onCreateOptionsMenu, onPrepareOptionsMenu and onOptionsItemSelected.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        ....
}

I'm dynamically loading this fragment using a FragmentTransaction in my Activity (that extends FragmentActivity).

However none of the menu callbacks (onCreateOptionsMenu, onPrepareOptionsMenu and onOptionsItemSelected) are being called (I've debugged with some breakpoints in those methods) and the menu isn't shown.

Am I missing something? Do I need to add something in my Activity?

I'm using the Android Compatibility Library, compiling with L11 SDK and testing in a Xoom.

EDIT: I've found the problem. My AndroidManifest is targeting L11, this seems to hide the menu button and prevent from the callbacks being called. However if I remove this from the manifest I loose some other features I need (for example the activated state in lists). Does anyone know how to solve this issue (enable the menu button) without removing the targetSdkVersion=11 from the Manifest?

12条回答
戒情不戒烟
2楼-- · 2019-01-17 06:16

I've found the problem. The AndroidManifest is targeting SDK 11, this seems to hide the menu button and prevent from the callbacks being called. I assume that this breaks the compatibility of the menu button that seems to be replaced by the action bar in Android 3.0

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-17 06:16

From the android developer site - link

Note: If you inflate menu items from a fragment, via the Fragment class's onCreateOptionsMenu() callback, the system calls onOptionsItemSelected() for that fragment when the user selects one of those items. However, the activity gets a chance to handle the event first, so the system first calls onOptionsItemSelected() on the activity, before calling the same callback for the fragment. To ensure that any fragments in the activity also have a chance to handle the callback, always pass the call to the superclass as the default behavior instead of returning false when you do not handle the item.

Therefore Marco HC is the best answer of all.

查看更多
聊天终结者
4楼-- · 2019-01-17 06:17

If you have an activity and a fragment that each loads menu items then you need to take special care of which overrides you use.

Activities can override onOptionsItemSelected and onMenuItemSelected, however fragments can only override onOptionsItemSelected.

If you override onMenuItemSelected in your activity and onOptionsItemSelected in your fragment, your fragment override will never get triggered.

Instead, use onOptionsItemSelected in both activity and fragment.

查看更多
5楼-- · 2019-01-17 06:20

If your toolbar is defined in the parent activity xml, make sure you do this in your fragment

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ....
    Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    setHasOptionsMenu(true);
}

And then of course, override onCreateOptionsMenu like below

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.edit_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

This is the only solution that worked for me!

查看更多
SAY GOODBYE
6楼-- · 2019-01-17 06:24

I think you have implemented onCreateOptionsMenu, onPrepareOptionsMenu and onOptionsItemSelected in the class that extends Fragment. Try by doing that in your Activity class where you are loading this fragment

查看更多
女痞
7楼-- · 2019-01-17 06:25

You need to make sure you call setHasOptionsMenu(true); onCreate or onCreateView is called in your fragment.

You also need to implement the override of onCreateOptionsMenu inside your fragment.

查看更多
登录 后发表回答