ActionBarSherlock Options menu not displayed on Google Maps fragment

272 Views Asked by At

I am using ActionBarSherlock to display fragmented tabs on Android 2.2 The problem I am having is that I am not able to display my custom OptionsMenu on the Google Maps fragment. I set setHasOptionsMenu(true); in the onCreate method, but it still does not appear.

This is my google maps fragment3.java

public class Fragment3 extends SherlockFragment {

    private MapView mMapView;
    private int group1Id = 1;

    int homeId = Menu.FIRST;
    int profileId = Menu.FIRST +1;
    int searchId = Menu.FIRST +2;
    int dealsId = Menu.FIRST +3;
    int helpId = Menu.FIRST +4;
    int contactusId = Menu.FIRST +5;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment3, container, false);

        mMapView = (MapView) view.findViewById(R.id.mapview);

        // inflat and return the layout
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
        GoogleMap googleMap = mMapView.getMap();
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(54.902815,23.9500459) , 16.0f) );
       // googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);

        return view;
    }

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

    /*
     * Using a mapview in a fragment requires you to 'route'
     * the lifecycle events of the fragment to the mapview
     */
    @Override
    public void onResume() {
        super.onResume();
        if (null != mMapView)
            mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mMapView)
            mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mMapView)
            mMapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != mMapView)
            mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (null != mMapView)
            mMapView.onLowMemory();
    }
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.action_about);
        menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.action_about);
        menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.action_about);
        menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.action_about);
        menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.action_about);
        menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.action_about);

        return onCreateOptionsMenu(menu); 
        }

       @Override
        public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

    case 1:
........

    case 6:
        //code here
    default:
        break;
           }
        return super.onOptionsItemSelected(item);
    }

}

does it not appear because it's in a fragment? Is there any way I can enable my own custom menu inside the google map fragment? All answers and discussions are welcome, let me know if you need to view any of my adapter classes that handle the fragments, or the .xml files.

1

There are 1 best solutions below

5
On

you could try to save your menu in menu folder and can inflate that menu like this

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