创建片段时未创建菜单但在恢复时工作

时间:2016-09-06 21:30:05

标签: java android android-fragments menu layout-inflater

我有一个显示mapview的片段。

我必须从片段中扩展菜单布局,并在代码中添加了setHasOptionsMenu(true),以便菜单栏可以从片段中膨胀。

但有趣的是,当片段第一次加载时它并没有显示出来。但是,如果我切换到其他片段并返回到此片段,菜单栏将再次显示。我不知道为什么它首先出现。

需要做什么?有人可以帮忙吗?

以下是片段布局的内容。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这是java类。

        public class SearchFragment extends Fragment implements GoogleMap.InfoWindowAdapter,
                GoogleMap.OnInfoWindowClickListener, LocationListener, OnMapReadyCallback {

          @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView()");

            rootView = inflater.inflate(R.layout.fragment_search, container, false);
            setHasOptionsMenu(true);
            MapsInitializer.initialize(this.getActivity());
            mMapView = (MapView) rootView.findViewById(R.id.map);
            mMapView.onCreate(savedInstanceState);

            mMapView.getMapAsync(this);

            this.markers = new HashMap<>();
            this.activity = (MainActivity) this.getActivity();

            return rootView;
        }

     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // Inflate the menu; this adds items to the action bar if it is present.
            Log.i(TAG, "onCreateOptionsMenu()");
            super.onCreateOptionsMenu(menu, inflater);
            menu.clear();
                this.activity.getMenuInflater().inflate(R.menu.menu_search, menu);

        }

 @Override
    public void onMapReady(GoogleMap map) {
        Log.i(TAG, "onMapReady");
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        try {
            if (checkScanPermissions()) {
                map.setMyLocationEnabled(true);
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(getLocation(), 18));
            }
        } catch (SecurityException e) {
            Log.e(TAG, "Permission to location not granted");
        }
        map.setBuildingsEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);

        map.setInfoWindowAdapter(this);
        map.setOnInfoWindowClickListener(this);

        this.mMap = map;

        setUpMap(false);
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i(TAG, "onLocationChanged");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate()");
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.i(TAG, "onSaveInstanceState()");
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        Log.i(TAG, "onLowMemory()");
        super.onLowMemory();
        mMapView.onLowMemory();
    }

    @Override
    public void onResume() {
        Log.i(TAG, "onResume()");
        super.onResume();
        setHasOptionsMenu(true);
        mMapView.onResume();
    }
}

2 个答案:

答案 0 :(得分:0)

您是否尝试删除menu.clear();来自onCreateOptionsMenu?

答案 1 :(得分:0)

这一行: setHasOptionsMenu(真); 是错误的方法。应该在onCreate中。

添加:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);
}