I have 3 Fragments and all of them in a single Mother activity. I want the following menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="@string/tipo_de_mapa_menu"
>
<menu>
<group
android:checkableBehavior="single"
>
<item
android:id="@+id/terreno_maptype"
android:title="@string/terreno_map_type"
/>
<item
android:id="@+id/satelite_maptype"
android:title="@string/satelite_map_type"
/>
<item
android:id="@+id/hibrido_maptype"
android:title="@string/hibrido_map_type"
/>
</group>
</menu>
</item>
<item
android:id="@+id/sobre_menu"
android:title="@string/sobre_menu"
/>
</menu>
in this Fragment:
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap map;
private View view = null;
private FloatingActionButton fab;
private LatLng yourCar;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.map_fragment, container, false);
setupComps();
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void setupComps() {
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(fabListener);
}
View.OnClickListener fabListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
centralizarACameraNoVeiculo();
}
};
public void configurarMapa() {
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setRotateGesturesEnabled(true);
}
public void alterarTipoParaHibrido() {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
public void alterarTipoParaTerreno() {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
public void alterarTipoParaSatelite() {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
public void centralizarACameraNoVeiculo() {
CameraPosition camera = new CameraPosition.Builder()
.target(yourCar)
.zoom(15)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(camera));
Toast.makeText(getActivity().getApplicationContext(), "Botão clicado...", Toast.LENGTH_SHORT).show();
}
public void apontarOMarker() {
// Add a marker in Maputo, Mozambique and move the camera
yourCar = new LatLng(-25.962803, 32.583162);
map.addMarker(new MarkerOptions().position(yourCar).title("Seu veículo está aqui"));
alterarTipoParaHibrido();
CameraPosition position = new CameraPosition.Builder()
.target(yourCar)
.zoom(15)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(position));
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
apontarOMarker();
configurarMapa();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.main_actvity_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.terreno_maptype:
//TODO
item.setChecked(true);
alterarTipoParaTerreno();
break;
case R.id.satelite_maptype:
//TODO
item.setChecked(true);
alterarTipoParaSatelite();
break;
case R.id.hibrido_maptype:
item.setChecked(true);
alterarTipoParaHibrido();
//TODO
break;
case R.id.sobre_menu:
Intent sobre = new Intent(getContext(), SobreActivity.class);
startActivity(sobre);
break;
}
return super.onOptionsItemSelected(item);
}
}
But, the problem is that this menu appears when in the remaining Fragments even without the menus have the methods onCreateOptionsMenu()
and onOptionsItemSelected()
The parent activity:
public class MainActivity extends AppCompatActivity {
private FragmentManager fragMan;
private FragmentTransaction ftrans;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.btm_route:
// TODO
getSupportActionBar().setTitle(R.string.rota_fragment_titulo_actionBar);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment, new RotaFragment())
.commit();
return true;
case R.id.btm_localizacao:
// TODO
getSupportActionBar().setTitle(R.string.localizacao_fragment_titulo_actionBar);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment, new MapFragment())
.commit();
return true;
case R.id.btm_fuel:
// TODO
getSupportActionBar().setTitle(R.string.fuel_fragment_titulo_actionBar);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment, new FuelFragment())
.commit();
return true;
}
return false;
}
};
public void setupComps() {
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setSelectedItemId(R.id.btm_localizacao);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public void configurarFragments() {
fragMan = getSupportFragmentManager();
ftrans = fragMan.beginTransaction();
ftrans.add(R.id.fragment, new MapFragment(), "MapaFragmento");
ftrans.commitAllowingStateLoss();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setTitle(R.string.localizacao_fragment_titulo_actionBar);
setContentView(R.layout.activity_main);
// configurarFragments();
if (savedInstanceState == null) {
//configurarFragments();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment, new MapFragment())
.commit();
}
setupComps();
}
}
How do I make this menu appear only in the specified Fragment?